''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Filtercab.vbs executes win32 "extract.exe" command ' on cab file, or all cab files in specified folder ' - displays filtered results of "extract.exe" STDIO ' ' extract.exe distributed with Win2000 Pro Resource Kit ' Also available for download via: ' http://www.microsoft.com/windows2000/techinfo/reskit/tools/existing/extract-o.asp ' ' M. Gallant 06/10/2002 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit Dim WshShell, StdOut, oExec, ExeOut, fso, oFolder Dim args, str, extractcmd Dim icab, cabarray Dim filterString : filterString = "security" Dim cabfile : cabfile = "D:\setup\WinBaseSDK-common.4.0.cab" Dim hits : hits = 0 Dim isFile : isFile = False Set WshShell = CreateObject("WScript.Shell") Set StdOut = WScript.StdOut Set args = WScript.Arguments If (NOT InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then WScript.Echo "******** Requires cscript.exe to run **********" WScript.Quit ' Terminate script. End If If args.Count >0 Then filterString = args(0) If args.Count > 1 Then cabfile = args(1) End If Else 'Requires at least filterstring argument ShowUsage WScript.Quit End If Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(cabfile) Then 'single cab file to search If NOT (fso.GetExtensionName(cabfile)="cab") Then WScript.Echo "File """ & cabfile & """ is not a cab file" WScript.Quit End If cabarray = Array(cabfile) isFile = True ElseIf fso.FolderExists(cabfile) Then 'folder to find cab files in Set oFolder = fso.GetFolder(cabfile) cabarray = GetCabFiles(oFolder) If NOT HasElements(cabarray) Then WScript.Echo "Folder """ & cabfile & """ contains no cab files" WScript.Quit End If Else WScript.Echo "File or folder """ & cabfile & """ not found" ShowUsage WScript.Quit End If If isFile Then WScript.Echo "Searching file """ & cabarray(0) & """ with filter """ & filterString & """ ..... " & vbCrLf Else WScript.Echo "Searching " & (Ubound(cabarray)+1) & " cab files in folder """ & _ cabfile & """ with filter """ & filterString & """ ..... " & vbCrLf End If For icab = 0 To Ubound(cabarray) hits=0 ' WScript.Echo cabarray(icab) extractcmd = "extract.exe /a /d " & """" & cabarray(icab) & """" Set oExec = WshShell.Exec("%comspec% /c " & extractcmd) Set ExeOut = oExec.StdOut Do While Not ExeOut.AtEndOfStream str = ExeOut.ReadLine If InStr(1, str, filterString, 1) <> 0 Then If hits=0 Then WScript.Echo cabarray(icab) StdOut.WriteLine " " & str hits = hits+1 End If Loop If hits >0 Then WScript.Echo hits & vbCrLf If Not oExec.StdErr.AtEndOfStream Then 'If Exec fails, display the STDERR stream. str = "STDERR: " + oExec.StdErr.ReadAll WScript.Echo str WScript.Quit End If Set oExec = Nothing Set ExeOut = Nothing Next Set WshShell = Nothing Set StdOut = Nothing Set fso = Nothing '----- GetCabFiles returns array of files in folder of type .cab ------ Function GetCabFiles(oFolder) Dim cabfiles(), oFile, files, count ' Redim cabfiles(0) Set fso = CreateObject("Scripting.FileSystemObject") Set files = oFolder.Files count=0 For Each oFile In files ' All files If fso.GetExtensionName(oFile.Name) = "cab" Then redim preserve cabfiles(count) cabfiles(count) = oFile.Path count = count+1 End If Next GetCabFiles = cabfiles End Function '---- HasElements detects if array has any elements defined ---- Function HasElements (Array) On Error Resume Next Dim elems : elems = LBound(Array) If Err.Number = 0 Then HasElements = True Else HasElements = False End If On Error Goto 0 End Function ' End HasElements Sub ShowUsage WScript.Echo vbCrLf & "--- Usage: filtercab.vbs filterstring [cabfile | cabsdirectory] " WScript.Echo "--- Requires WSH 5.6, and extract.exe utility" WScript.Echo "--- Launch with cscript.exe " & vbCrLf WScript.Echo "Examples: " WScript.Echo " filtercab java C:\developer\mycustom.cab" WScript.Echo " [searches cab file ""mycustom.cab"" for ""java"" string]" & vbCrLf WScript.Echo " filtercab vbs D:\setup" WScript.Echo " [searches all cab files in ""D:\setup"" directory for ""vbs"" string]" End Sub '------------- End Script --------------------