API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Get List Of Files
I recently had a need to get a list of files starting in some directory. I wanted to get all the files in that directory and any/all subdirectories under that main directory. This subroutine will scan for files and return a List object of all the file names.

Sub GetListOfFiles(inputDir As String, filesList As Byte, fileCount As Integer, errors As Variant)
   On Error GoTo BubbleError
   ' Scan through the directory and get the list of files in that directory. If there are any
   ' subdirectories, also scan through those by calling this subroutine recursively.
   Dim startingDir As String
   Dim slash As String
   Dim fileName As String
   Dim i As Integer
   Dim dirListList As Byte
   
   startingDir = inputDir
   ' Make sure the directory ends with a slash
   If InStr(startingDir, "\") <> 0 Then slash = "\" Else slash = "/"
   If Right(startingDir, 1) <> slash Then startingDir = startingDir & slash
   
   Print "Reading directory " & startingDir & " for files..."
   
   ' Scan the directory for files (0) and subdirectories (16)
   fileName = Dir$(startingDir & "*.*", 0+16)
   While fileName <> ""
      If fileName <> "."And fileName <> ".." Then
         ' Check if the file name is too long. If it is, add it to the errors array and
         ' don't process it
         If ( (Len(fileName)+Len(startingDir) >= 259) And (InStr(fileName, slash) = 0) ) _
         Or (Len(fileName) >= 256) Then
            If Not IsArray(errors) Then
               ReDim errors(0) As String
               i = 0
            Else
               i = UBound(errors)+1
               ReDim Preserve errors(i)
            End If
            errors(i) = {File name "} & fileName & {" is too long and will not be processed.}
           
         Else ' File name isn't too long
           
            ' Make sure that "fileName" contains the full path to the file
            If InStr(fileName, slash) = 0 Then fileName = startingDir & fileName
            ' Is the returned full path a directory?

            ' Note that GetFileAttr is the sum of several properties, including 16 = directory.
            ' 1 = Read only file (which is fine, we're going to read it only). 2 = hidden file.
            ' 4 = System file. 32 = File that has changed since it was last backed up.
            i = GetFileAttr(fileName)
            If i >= 32 Then i = i-32 ' Eliminate the "file has changed" flag if it's present
            If (i And 16) = 16 Then ' It is a directory
               dirList(fileName) = 1 ' Store the directory name for later processing
            Else ' Not a directory, so it must be a file - add it to our List element
               files(fileName) = 1
               fileCount = fileCount+1
            End If
           
         End If ' Ends check to see if file name is too long
      End If ' Ends check to skip the "." and ".." "files" that get returned from Dir$
      fileName = Dir$() ' Get the next file/directory from the input directory
   Wend
   
   ' Run through all the directories found above
   ForAll dirName In dirList
      Call GetListOfFiles(ListTag(dirName), files, fileCount, errors)
   End ForAll
   
   Exit Sub
BubbleError:
   Error Err, Error$ & Chr$(10) & "in subroutine " & Getthreadinfo(1) & ", line " & Cstr(Erl)
End Sub

You can check if any files were found by looking at the fileCount variable. (You can also use that value to print out "found xxx files" messages to a log routine). If there are errors during processing, they will be returned in the errors variable. You can check to see if that's an array to indicate if there were any errors found.