Create Directory Listing of Files In App Engine

Need to know dynamically which files are in a given folder?

May 2018 Update:  You can use function FindFiles to do this.

Also, apologies on the Ampersand.  We are working on making it not be &.

 &file_names = FindFiles(&file_loc, %FilePath_Absolute);

The code further in this article is the old way to do this.

The code below will allow you to list all of the files in a given directory, then allow you to open each file.  It could be useful to read which files have been recently added to an upload folder.  I used this code to read which files had recently been placed in the process scheduler working folder (first SQLExec in code example).

Details:  We run a system command to get a directory listing, and we send that output to a new file, in the same directory.  Then we open that new file and review which files were found.

/*Setup the Slash variable*/
If &OS = "Windows" Then 
    &slash = "\";
else 
    &slash = "/";
End-if;
/*Get the current working folder - You may want to use a static folder where files are being uploaded to - If you use this SQLExec, process instance &ProcInst*/
SQLExec("SELECT PRCSOUTPUTDIR FROM PSPRCSPARMS WHERE PRCSINSTANCE = :1", &ProcInst, &DirectoryLocation); 

/*Setup the filename to output to.*/
&DirectoryFileName = "FileListing_" | &ProcInst | ".filelisting"; 

/*Setup the folder to write the file to*/ 
&DirectoryListing = &DirectoryLocation | &slash | &DirectoryFileName; 

If &OS = "Windows" Then 
    &CMD_TO_RUN = "cmd.exe /c dir """ | &DirectoryLocation | """ /b > """ | &DirectoryListing | """";
Else 
    &CMD_TO_RUN = "ls """ | &DirectoryLocation | """ > """ | &DirectoryListing | """";
End-If; 

/*It is always best to Commit the work before running Exec*/ 
CommitWork();
try 
    &return_value = Exec(&CMD_TO_RUN, %Exec_Synchronous + %FilePath_Absolute); 
catch Exception &e 
    MessageBox(0, "", 0, 0, "Error Found. Exception is " | &e | " with return value " | &return_value); 
    MessageBox(0, "", 0, 0, "Command sent was " | &CMD_TO_RUN); 
    MessageBox(0, "", 0, 0, "Going to Next step in App Engine."); 
end-try; 

Local File &f1, &f2; 
&f1 = GetFile(&DirectoryListing, "r", %FilePath_Absolute); 
While &f1.ReadLine(&RowOfData); 
/*Skip the file if the filename matches our Directory File name*/ 
    If &RowOfData <> &DirectoryFileName Then
        MessageBox(0, "", 0, 0, "Processing Filename " | &RowOfData); 
        &Columns_array = Split(&RowOfData, "."); /*Useful to read the file extension. Use variable Upper(&Columns_array [2])*/ 
        &f2 = GetFile(&DirectoryLocation | &slash | &RowOfData, "r", %FilePath_Absolute);
        While &f2.ReadLine(&F2RowOfData);
            /*Process the row of data, using variable &F2RowOfData*/
        End-While;
    End-If;
End-While;

&f1.Close();

↑ Back to top