troubleshooting Question

VB Script to search multiple folders for specific file type

Avatar of BlizzardMN
BlizzardMNFlag for United States of America asked on
NetworkingScripting LanguagesVB Script
12 Comments1 Solution2528 ViewsLast Modified:
I have a script that will search a drive letter for a specified file type and export the results to a csv file.  I like how all the current formatting is with the csv file listing location, size, date created, ect. but here is what I need to be able to do and need a little help and direction.

I have voice files out in some of our network user folders that we need to find easily sometimes on a regular basis.  I don't want to search the whole user directory share since that would be to huge to do.  I would like to have a text file with a list of usernames(folder paths) that the script would reference to search those folders only or folders like those using a wildcard.

I hope that makes sense!  Here is my current code.
On error Resume Next
 
Dim oWmi
Dim oRef
Dim fso,f

strFileName = "FoundUserFiles"

'file extension type to look for 
strType="dss"

'drive letter to search
strDrive="F:"

'output location of the csv file 
strOutput="c:\test2\" & strFileName & ".csv"
 
strQuery="Select Name,CreationDate,LastAccessed,LastModified," & _
"FileSize,Extension,Drive FROM CIM_DATAFILE WHERE Extension='" & strType & _
 "' AND Drive='" & strDrive & "'"
 
Set fso=CreateObject("Scripting.FileSystemObject")
 If fso.FileExists(strOutput) Then fso.DeleteFile(strOutput)
Set f=fso.CreateTextFile(strOutput)
 If Err.Number Then
  wscript.echo "Could not create output file " & strOutput
  wscript.quit
 End If
 
Set oWmi=GetObject("winmgmts:")
If Err.Number Then
  strErrMsg= "Error connecting to WINMGMTS" & vbCrlf
  strErrMsg= strErrMsg & "Error #" & err.number & " [0x" & CStr(Hex(Err.Number)) &"]" & vbCrlf
        If Err.Description = "" Then
            strErrMsg = strErrMsg & "Error description: " & Err.Description & "." & vbCrlf
        End If
  Err.Clear
  wscript.echo strErrMsg
  wscript.quit
End If
 
Set oRef=oWmi.ExecQuery(strQuery) 
If Err.Number Then
  strErrMsg= "Error connecting executing query!" & vbCrlf
  strErrMsg= strErrMsg & "Error #" & err.number & " [0x" & CStr(Hex(Err.Number)) &"]" & vbCrlf
        If Err.Description = "" Then
            strErrMsg = strErrMsg & "Error description: " & Err.Description & "." & vbCrlf
        End If
  Err.Clear
  wscript.echo strErrMsg
  wscript.quit
End If

 
f.Writeline "FilePath,Size(bytes),Created,LastAccessed,LastModified"
 
For Each file In oRef
 f.Writeline file.Name & "," & file.FileSize & "," & ConvWMITime(file.CreationDate) & _
  "," & ConvWMITime(file.LastAccessed) & "," & ConvWMITime(file.LastModified)
Next
 
f.Close
 
'wscript.echo "Finished.  See " & strOutput & " for results"
 
Set oWmi=Nothing
Set oRef=Nothing
Set fso=Nothing
Set f=Nothing
 
wscript.quit
 
'************************************************************************************
' Convert WMI Time Function
'************************************************************************************
Function ConvWMITime(wmiTime)
On Error Resume Next
 
yr = left(wmiTime,4)
mo = mid(wmiTime,5,2)
dy = mid(wmiTime,7,2)
tm = mid(wmiTime,9,6)
 
ConvWMITime = mo & "/" & dy & "/" & yr & " " & FormatDateTime(left(tm,2) & _
":" & Mid(tm,3,2) & ":" & Right(tm,2),3)
 
End Function
 
'EOF
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros