How do I create a directory through the Harbour language?
How do I create a directory through the Harbour language?
For example, I want to create a folder temp
inside the current directory from where the program is running.
3 answers
First of all, I do not recommend creating temporary things in the program folder. The OS usually already has a definition 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
Which is equivalent to the old Clipper MakeDir
. Interestingly, when flag HB_COMPAT_C53
is active when building Harbour, it activates this line:
HB_FUNC_TRANSLATE( MAKEDIR, HB_DIRCREATE )
this stands at src/rtl/dirdrive.c
If you want a more complex path (creation recursive):
hb_DirBuild( 'caminho/composto')
About observing the location of the temp, think that often the application is installed in a location, such as an SSD, and the temporary in a normal HD to avoid premature wear, or even in a RamDisk for higher execution speed. It would be nice to respect the preferences of the system administrator.
To get the location of the system temp
:
cLocal := hb_DirTemp()
And it also has hb_FTempCreate
, which already takes care of all this that 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 the address https://harbour.github.io/doc , but it is in a general reorganization process, a bit confusing to leave permalinks at the moment
Using the function MakeDir()
. So just do:
MakeDir("caminho desejado").
Remembering that Harbour is case-insentive and in thesis you can write the name of the function as you like. It returns 0 if the creation occurred ok, or the error number returned by the operating system. Harbour does not usually use exceptions to report errors, except to have a mechanism for this. The function FError()
it can be used as an auxiliary.
The function is compatible with Clipper. There are some function aliases.