Create a Directory
How can i create a directory when the app begins but validating if the directory exists avoiding an error code?
thanks a lot!
[130 byte] By [
Nora] at [2008-2-6]
Correct. You can use MKDIR and MD to create a folder. Of course you'd put this command inside a TRY...CATCH block:
IF
NOT DIRECTORY("d:\my folder")
TRY
MD "d:\my folder"
CATCH TO ex
* handle error here
MESSAGEBOX(ex.message, 48, _screen.Caption)
ENDTRY
ENDIF
Hi Nora,
DIRECTORY() isn't a good idea to validate whether a directory exists, or not. You can easily see this with code like this:
? GETENV("APPDATA")
? DIRECTORY(GETENV("APPDATA")) The DIRECTORY() function fails when any of the directories in the tree is hidden or system. Instead use this DirectoryX function:
*========================================================================================
* Returns .T. if a directory exists. Unlike the built-in function this routine still
* works if one of the directories has the hidden or system file attribute set.
*========================================================================================
Procedure DirectoryX
LParameter tcDirectory
*--
* We assume that the directory doesn't exist.
*--
Local llExist
llExist = .F. *--
* If the directory is just a drive letter or a share (C:\ or \\server\share), we cannot
* use the ADIR() solution. In this case we switch back to DIRECTORY(). With UNC paths
* the directory name must be terminated with a backslash in order for DIRECTORY() to
* coirrectly return .T. or .F.
*--
If not m.llExist
Do case
Case Len(m.tcDirectory) <= 3 and Substr(m.tcDirectory,2,1) == ":"
llExist = Directory( Left(m.tcDirectory,1)+":\" )
Case Left(m.tcDirectory,2) == "\\" and Occurs("\",m.tcDirectory) == 3
llExist = Directory( m.tcDirectory+"\" )
Case Left(m.tcDirectory,2) == "\\" ;
and Occurs("\",m.tcDirectory) == 4 ;
and Right(m.tcDirectory,1) == "\"
llExist = Directory( m.tcDirectory )
EndCase
EndIf
*--
* If this is any other kind of directory we check with ADIR()
*--
Local laDir[1]
If not m.llExist
If ADir(laDir,m.tcDirectory,"SDH") > 0
If "D" $ laDir[1,5]
llExist = .T.
EndIf
EndIf
EndIf
Return m.llExist
Hi Nora,
two more things to consider:
1) MKDIR only creates directories one level deep. If C:\Customer1\Data doesn't exist at all, you first have to create the Customer1 directory, then the Data directory.
2) When creating directories pay attention to the permissions of the current users. Only adminsitrators can create directories as they want. Regular users can only create directories in their profile and on non-system drives. If you ever want to get your application certified, you should ensure that your setup is creating all the directories the application needs, not the application itself. The only directories that an application usually needs to create at runtime are those in the temporary directory.
Hi Christof,
Your #1 comment is not true.
MKDIR
"C:\Customer1\Data"
?DIRECTORY("C:\Customer1\Data") && .T.And in VFP8 and up, you can pass a second parameter to DIRECTORY():
?
DIRECTORY(GETENV("APPDATA"),1) && .T.
Hi Eric,
found it. MD fails to create a directory of more than one level, when the parent directory is either hidden or a system directory:
Md (Addbs(GetEnv("APPDATA"))+Sys(2015)+"\"+Sys(2015)) Creating the directory one level at a time works, though.