(view source code of wildcards.vbs as plain text)
Option Explicit
WScript.Echo Join( GetFiles( WScript.Arguments.Unnamed.Item(0) ), vbCrLf )
Function GetFiles( strFilespec )
' Name : GetFiles
' Function : List all files matching the specified filespec, all DOS wildcards allowed
' Returns : An array of fully qualified paths of all matching files, or a single element array with the text "Error"
' Remarks : If no folder is specified, the current directory will be assumed.
' DOS wildcards "*" and "?" are allowed in the FILE name and/or extension, but NOT in the DIRECTORIES.
' E.g. "D:\folder\*file*_*.??" is allowed, but "D:\folder\*\file*.??" is NOT.
' Author : Rob van der Woude, http://www.robvanderwoude.com
' Version : 1.00, 2017-01-31
Dim colFiles, objFile, objFolder, objFSO, objRE, wshShell
Dim strFiles, strFolder, strPattern
GetFiles = Array( "Error" )
' Return "Error" if no filespec is specified
If Trim( strFilespec ) = "" Then Exit Function
' Handle (unlikely) error in specified UNC path
Set objRE = New RegExp
objRE.Pattern = "^\\\\" ' Check if filespec starts with double backslash
If objRE.Test( strFilespec ) Then
' Check if filespec is a valid UNC path: \\server\share\relativepath
objRE.Pattern = "^\\\\[\w-]+\\[\w\$-]+\\[^\\]"
If Not objRE.Test( strFilespec ) Then
Set objRE = Nothing
Exit Function
End If
End If
Set wshShell = CreateObject( "WScript.Shell" )
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
If InStr( strFilespec, "\" ) And Len( strFilespec ) > 1 Then
' If filespec starts with single backslash, prefix it with current directory's drive
If Left( strFilespec, 1 ) = "\" And Not Left( strFilespec, 2 ) = "\\" Then
strFilespec = objFSO.GetDriveName( wshShell.CurrentDirectory ) & strFilespec
End If
' Split filespec into parent directory and actual FILE spec
strFolder = Mid( strFilespec, 1, InStrRev( strFilespec, "\" ) )
strFilespec = Mid( strFilespec, InStrRev( strFilespec, "\" ) + 1 )
End If
' Assume current directory if no parent directory is specified
If strFolder = "" Then strFolder = wshShell.CurrentDirectory
' Quit if folder does not exist
If Not objFSO.FolderExists( strFolder ) Then
Set objRE = Nothing
Set objFSO = Nothing
Set wshShell = Nothing
Exit Function
End If
' Convert DOS wildcards to regex pattern
objRE.Pattern = "([\.\(\)\[\]\{\}\$])"
objRE.Global = True
objRE.IgnoreCase = True
strPattern = objRE.Replace( strFilespec, "\$1" )
strPattern = Replace( strPattern, "?", "[^\\]" )
strPattern = Replace( strPattern, "*", "[^\\]*" )
objRE.Pattern = "(^|\\)" & strPattern & "$"
' Get a collection of files
Set objFolder = objFSO.GetFolder( strFolder )
Set colFiles = objFolder.Files
strFiles = ""
' Iterate through the list of files
For Each objFile In colFiles
' Check if the file name matches filespec
If objRE.Test( objFile.Path ) Then
' Add the file to the list
strFiles = strFiles & ";" & objFile.Path
End If
Next
' Return the list of files as an array
GetFiles = Split( Mid( strFiles, 2 ), ";" )
' Cleanup
Set colFiles = Nothing
Set objFolder = Nothing
Set objRE = Nothing
Set objFSO = Nothing
Set wshShell = Nothing
End Function
page last modified: 2024-04-16; loaded in 0.0074 seconds