' filter.vbs ' Lets you query for files using wildcards ' ------------------------------------------------------------------------ ' Get the name of the folder to query Dim dirName If WScript.Arguments.Count = 0 Then dirName = InputBox("Enter the FULLY-QUALIFIED name of the folder:", _ , dirName) filespec = InputBox("Enter the file specification:", , filespec) Else dirName = WScript.Arguments.Item(0) filespec = WScript.Arguments.Item(1) End if If dirName = "" Or filespec = "" Then WScript.Quit End If ' Get the filtered collection and dump out the filenames Set coll = FileQuery(dirName, filespec) If coll.Count > 0 then msg = "" For Each file In coll msg = msg & vbCrLf & file Next MsgBox msg End If Function FileQuery(dirName, filespec) ' Creates the required objects Set fso = CreateObject("Scripting.FileSystemObject") Set dic = CreateObject("Scripting.Dictionary") ' Gets the Files collection for the folder If Not fso.FolderExists(dirName) Then Exit Function End If Set f = fso.GetFolder(dirName) Set files = f.Files ' Enumerates and processes the files For Each file In files fileName = file.Name If IsLike(fileName, filespec) Then dic.Add file.Name, file.Name End If Next Set FileQuery = dic End Function Function IsLike(strText, match) Dim i, str, spec, temp, token, nPos ' Turn strings to lower case str = LCase(strText) spec = LCase(match) ' Split the various components of the match string aInput = split(spec, "*") ' "c*.*m" becomes Array("c", ".", "m") ' Walk the array of specification sub-components i = 0 For Each token In aInput ' The first token plays an important role: the file name must begin ' with a substring identical to the token. If i = 0 Then temp = Left(str, Len(token)) ' Don't match... If temp <> token Then IsLike = False Exit Function End If ' Removes the leading substring before next step str = Right(str, Len(str) - Len(token)) Else temp = str ' For each asterisk we come accross, we chack that what remains of ' the filename contains the next token of the match string. nPos = Instr(1, temp, token) ' Don't match... If nPos = 0 Then IsLike = False Exit Function End If ' Removes the leading substring before next step str = Right(str, Len(str) - nPos + 1) End If i = i + 1 Next IsLike = True End Function