VBScript to push files into an array based on a wildcard


Option Explicit
Dim gFSO
Dim dirListing
Dim fileName

set gFSO = CreateObject("Scripting.FileSystemObject")

dirListing = ListDir("c:??*d*")

If UBound(dirListing) = -1 then
Wscript.Echo "No files found."
Else
For Each fileName in dirListing
WScript.Echo FileName
Next
End If

'==============================================================================
' List a directory, with the last part of the directory being the path
'==============================================================================
Function ListDir (ByVal Path)
Dim fileRegex
Dim searchFolderName
Dim searchFolder
Dim searchFolderFiles
Dim fileName
Dim fileArray
Dim file

set fileRegex = New RegExp
If gFSO.FolderExists(Path) Then ' Path is a directory, list all files in path
searchFolder = Path
fileName = ""
Else
searchFolderName = gFSO.GetParentFolderName(Path)
fileName = gFSO.GetFileName(Path)
fileRegex.Global = True
fileRegex.Pattern = "."
fileName = fileRegex.Replace(fileName, ".")
fileRegex.Pattern = "?"
fileName = fileRegex.Replace(fileName, ".")
fileRegex.Pattern = "*"
fileName = fileRegex.Replace(fileName, ".*")
End If
With fileRegex
.Pattern = fileName
.IgnoreCase = True
.Global = False
End With

ReDim fileArray(1)
Dim fileCount : fileCount = 0

Set searchFolder = gFSO.GetFolder(searchFolderName)
Set searchFolderFiles = searchFolder.Files

For Each file in searchFolderFiles
If fileRegex.Test(file.Name) Then
If fileCount > UBound(fileArray) Then
ReDim Preserve fileArray(fileCount*2)
End If
fileArray(fileCount) = file.Path
fileCount = fileCount + 1
End If
Next
ReDim Preserve fileArray(fileCount - 1)
ListDir = fileArray
End Function