PDA

View Full Version : Tuned Dir_ListArray to accept multiple masks



Petr Schreiber
02-04-2013, 16:23
Sometimes it is useful to have list of files satisfying multiple masks, I did a routine to pack it together in one array:

Code:


' Requires : FILE module
' -----------------------------------------------------------------------------
' Description: Function to list files of given type, with option of specifying multiple masks
' -----------------------------------------------------------------------------
' Parameters :
' -----------------------------------------------------------------------------
' sAllFiles : Array which will be filled with files
' sDirectory : Directory, where the search is performed
' sMasks : Mask of the file(s). For multiple files, separate using |, for example "*.jpg|*.png"
' nFlags : Flags, such as %FILE_NORMAL - the same as for classic DIR_ListArray
Function Dir_ListArrayMulti(ByRef sAllFiles() As String, sDirectory As String, sMasks As String, nFlags As Long) As Long

' -- Extract masks
String singleMask()
Long maskCount = Parse(sMasks, singleMask, "|")

Long maskID, arrayID, fileID, minIndex, totalCount

' -- Temporary arrays to hold values of single mask
String sFiles()
Long nFiles

' -- Going for each mask
For maskID = 1 To maskCount
nFiles = DIR_ListArray(sFiles, sDirectory, singleMask(maskID), nFlags)

' -- Processing files for single mask
If nFiles Then
' -- Keeping the total count
totalCount = minIndex + nFiles

' -- Making enough space for it
ReDim Preserve sAllFiles(totalCount)

' -- Appending it to the list of all files
fileID = 1
For arrayID = minIndex+1 To totalCount
sAllFiles(arrayID) = sFiles(fileID)

fileID += 1
Next

' -- Updating last index in the list of all files
minIndex = totalCount
End If
Next

' -- Returning the number of all files, the array is passed out byref
Return totalCount

End Function


Sample usage:


String sFiles()
' -- This will fill the array with images from both Canon and Nikon cameras, notice the mask separation using |
Long nFiles = Dir_ListArrayMulti(sFiles, APP_SourcePath, "IMG_*.JPG|DSC_*.JPG", %FILE_NORMAL)



Petr