How do I create a directory using Harbor language?
For example, I want to create a folder temp
within the current directory where the program is running.
How do I create a directory using Harbor language?
For example, I want to create a folder temp
within the current directory where the program is running.
First of all, I do not recommend creating temporary things in the program folder. The OS usually already has a setting of temp
for this.
The function to create a directory is
hb_DirCreate( 'temp' )
Example:
IF hb_DirCreate( cDir ) == 0
? "Diretório criado com sucesso"
ENDIF
That is equivalent to the old MakeDir
of Clipper. Of curiosity, when the HB_COMPAT_C53
flag is active when building Harbor, it activates this line:
HB_FUNC_TRANSLATE( MAKEDIR, HB_DIRCREATE )
This is in src/rtl/dirdrive.c
If you want a more complex path (recursive creation):
hb_DirBuild( 'caminho/composto')
On observing the temporary location, think that the application is often installed in a location, such as an SSD, and temporary in a normal HD to avoid premature wear, or even in a RamDisk for faster execution speed. It would be good to respect the preferences of the system administrator.
To get the location of temp
system:
cLocal := hb_DirTemp()
And also has hb_FTempCreate
, which already takes care of all this I mentioned and creates the temporary file, and is abstracted by
TempFile([<cDirectory>], [<cExtension>], [<nFileAttr>])
The documentation is at link , but is in a general reorganization process, a little confusing for leave permalinks right now
Using the MakeDir()
function. So just do it:
MakeDir("caminho desejado").
Remembering that Harbor is case-insentive and in thesis you can write the name of the function however you want. It returns 0 if the creation occurred ok, or the number of the error returned by the operating system. Harbor does not use exceptions to report errors , to have a mechanism for this. The FError()
function can be used as an aid.
The function is compatible with Clipper. There are some function aliases.
One way is to use MAKEDIR
MAKEDIR("temp")