Pages

8/31/2007

Windows::Inventory::Report files of particular extension::UPDATE


I seem to make this mistake a lot. My initial goal was to create a csv format file and import to excel or someplace. But I did not account for the case where a comma is in the data. It is never in the front of my mind that a comma is a valid character in a filename.
The corrected script is below.

'==========================================================================
' NAME: Script to search for files with listed extensions
'
'
'==========================================================================

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Const PATH_TO_INPUT = "in.txt"
Const PATH_TO_OUTPUT = "out.txt"

Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Dim shl
Set shl = WScript.CreateObject("WScript.Shell")

Dim input
Set input = fso.OpenTextFile(PATH_TO_INPUT)

Dim output
Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)

Dim wmiService
Dim wmiResults
Dim objwMIService
Dim colFiles
Dim objFile

Dim hostname

Dim line
Dim exec
Dim pingResults
Dim strFileName


While Not input.AtEndOfStream
line = input.ReadLine
hostname = ""
Set exec = shl.Exec("ping -n 2 -w 500 " & line)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, "reply from") Then

WScript.Echo "Reply From: " & line
hostname = line

Set objWMIService = GetObject("winmgmts:\\" & hostname & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where Extension = 'pst' OR Extension = 'pdf' OR Extension = 'doc' OR Extension = 'xls'")

For Each objFile in colFiles
strFileName = Replace(objFile.Name, "," , " ")
output.WriteLine hostname & "," & strFileName & "," & objFile.FileSize
Next

Else
WScript.Echo line & " no response"
End If
Wend

output.Close
input.Close

Set wmiService = Nothing
Set wmiresults = Nothing

No comments: