Info
Content

Check if a Directory Exists


How do you check if a directory exists on the application server?

The following code is one way I've found. It uses the FindFiles function to return an array of subdirectories (matching on the * wildcard) &aSubDirs in a base directory &sBaseDir. It then loops through all the subdirectories and compares them to the check directory (&sCheckDir). Note that in this example, I am using absolute file paths. This should work on both NT and Unix servers (with appropriate file path changes).

ocal array of string &aSubDirs;
Local string &sBaseDir;
Local string &sCheckDir;
Local boolean &bDirExists = False;
Local integer &i;
 
&sBaseDir = "C:\TEMP";
&sCheckDir = "C:\TEMP\TEST";
 
/* Get all the directories under the base directory &sBaseDir (C:\TEMP) */
&aSubDirs = FindFiles(&sBaseDir | "*", %FilePath_Absolute);
 
/* Loop through sub-directories and check if any of them match the directory &sCheckDir (C:\TEMP\TEST) */
For &i = 1 to &aSubDirs.Len
    If &aSubDirs[&i] = &sCheckDir Then
        &bDirExists = True;
    End-If;
End-For;
 
If &bDirExists = True Then
    /* The directory exists */
Else
    /* The directory does not exist */
End-If;
No Comments
Back to top