PeopleSoft - Check if Files, Folder / Directory Exists


Check if a File Exists

Use the FileExists function to determine whether a particular external file is present on your system, so you can decide which mode to use when you open the file for writing.

If FileExists("c:\mydir\myfile.txt", %FilePath_Absolute) Then
   &MYFILE = GetFile("c:\mydir\myfile.txt", "A");
   /* Process the file */
End-If;

Check if a Folder / Directory Exists


Method 1:
Assumption: you have at least 1 file in directory otherwise CreateDirectory error out. Or you can catch exception at CreateDirectory function.

Local array of string &dirExist;
&dirExist = FindFiles(&filedir | "\*.*", %FilePath_Absolute);
If &dirExist.Len = 0 Then
   CreateDirectory(&filedir, %FilePath_Absolute);
End-If

Method 2
(Thanks to Wikidot)
Local 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;