<< Click to Display Table of Contents >> Navigation: ThinBASIC Modules > File > DIR: functions working on directories > DIR_ListArray |
Description
Fill an array variable with the list of files found in the specified path having the requested characteristics.
Function will also return number of files found.
Syntax
nFiles = DIR_ListArray(StringArrayVariable, Path, Mask, [Attribute, [Options]])
Returns
Number: number of files found.
Parameters
Name |
Type |
Optional |
Meaning |
StringArrayVariable |
String |
No |
An already defined string array variable that will receive file names found |
Path |
String |
No |
Path to search into. Examples: "C:\Temp\" "C:\"
|
Mask |
String |
No |
Specifies a filename or additional sub path which can include a drive name and OS compatible wildcard characters (* and ?). Examples: "*.*" "*.TXT" "*.dll;*.exe"
In this case, multiple scans will take place and files will be added to array sequentially. It is responsibility of the programmer to sort returned array if needed |
Attribute |
Numeric |
Yes |
See File Equates for possible attributes. Examples: %FILE_NORMAL Or %FILE_ADDPATH %FILE_SYSTEM Or %FILE_HIDDEN Or %FILE_ADDPATH
Attention: %FILE_ADDPATH do not work alone but must be always used in conjunction with another attribute. |
Options |
Numeric |
Yes |
Not used. Possible future usage. |
Remarks
This function will automatically dimension StringArrayVariable based on the number of files found.
In case of multiple file masks (see Mask parameter above), multiple scans can produce the same file name multiple times. For example "*.JPG;*.*G" can produce the same file name multiple times. In this case DIR_ListArray will automatically take care to remove duplicated file names from the array.
Restrictions
If no files will be found, in any case StringArrayVariable will be dimensioned to 1 but function will return zero.
See also
Examples
Sample script
USES "FILE"
Dim MyList() As String
Dim nFiles As Long
nFiles = DIR_ListArray(MyList, "C:\Temp\", "*.*", %FILE_ADDPATH)
MSGBOX 0, "Number of files found: " & nFiles
'---MyList will now contains the list of files found
Sample script
uses "CONSOLE"
uses "FILE"
dim sFiles() as String
dim nFiles as Long
'---Multiple Masks specified
nFiles = DIR_ListArray(sFiles(), App.Environ.WinDir + "\System32\", "*.txt;*.exe")
'---File are by default sorted by Mask execution and then by file name
'---Here we Sort globally by file name
array Sort sFiles()
printl join$(sFiles, $crlf)
'---Wait for user to press a key
waitkey(0, "---Press a key to end")
Thanks to Abraxas for the following script example
USES "UI"
USES "FILE"
Dim SelectedDIR As String ' The directory that we want to scan
Dim TheFileList() As String ' The Filename Table
Dim FileLength() As DWORD ' The File Length Table
Dim NumberofFiles As DWORD ' The Number of files in the list
Dim n As Long ' Loop Variable
Dim sMsg As String ' Message String
SelectedDIR = DIALOG_BrowseForFolder(0, "Please select a directory", "C:\", %False)
If SelectedDIR <> "" Then
NumberofFiles = DIR_ListArray(TheFileList, SelectedDIR, "*.*", %FILE_NORMAL Or %FILE_ADDPATH)
' Fill File Length Table
ReDim FileLength(NumberofFiles) ' Allocate space for File Length Table
For n = 1 To NumberofFiles
FileLength(n) = File_SIZE (TheFileList(n))
Next
sMsg = "Number of files found: " & NumberofFiles & $CRLF & $CRLF
sMsg += "First 10 files are:" & $CRLF & $CRLF
sMsg += JOIN$(TheFileList, $CRLF, "", 1, 10) ' add only 10 files to list
Else
sMsg += "No selected folder. Operation not performed."
End If
MSGBOX 0, sMsg