How do I create a directory through the Harbour language?

Asked

Viewed 460 times

3

How do I create a directory through the Harbour language?

For example, I want to create a folder temp within the current directory from which the program is running.

3 answers

5


First of all, I don’t recommend creating temporary things in the program folder. OS usually already has a definition of temp for that reason.

The function to create a directory is

hb_DirCreate( 'temp' )

Example:

IF hb_DirCreate( cDir ) == 0
   ? "Diretório criado com sucesso"
ENDIF

Which equals the old MakeDir of the Clipper. Of curiosity, when the flag HB_COMPAT_C53 is active when building Harbour, it activates this line:

HB_FUNC_TRANSLATE( MAKEDIR, HB_DIRCREATE )

that stays in src/rtl/dirdrive.c

If you want a more complex path (recursive creation):

hb_DirBuild( 'caminho/composto')

Regarding the location observation of temporary, think that often the application is installed in a location, such as an SSD, and the temporary in a normal hard drive to avoid premature wear, or even in a Ramdisk for faster speed of execution. It would be good to respect the preferences of the system administrator.

To get the location of temp of the system:

cLocal := hb_DirTemp()

And there’s also the hb_FTempCreate, who already takes care of all this I mentioned and creates the temporary file, and is abstracted by

TempFile([<cDirectory>], [<cExtension>], [<nFileAttr>])

https://harbour.github.io/doc/clct3.html#tempfile


The documentation is at https://harbour.github.io/doc, but is in a process of general reorganization, a little confused to leave permalinks at the moment

4

Using the function MakeDir(). So just do it:

MakeDir("caminho desejado").

Remembering that Harbour is case-insentive and in thesis you can write the name of the function as you want. It returns 0 if the creation occurred ok, or the error number returned by the operating system. The Harbour does not usually use exceptions to report errors, even having a mechanism for it. The function FError() can be used in an auxiliary way.

The function is compatible with Clipper. There are some aliases of the function.

2

One way is by using the function MAKEDIR

MAKEDIR("temp")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.