Link to home
Start Free TrialLog in
Avatar of sydpolo
sydpolo

asked on

VBSCRIPT search for file type

Hello,
Looking for a script to search in a particular folder (which has many subfolders) for media file types such as mp3, wmv etc.. and store the result on a text file.

Thank you.

Avatar of chandru_sol
chandru_sol
Flag of India image

Try this.............


regards
Chandru
'Script starts here
On error Resume Next
 
Dim oWmi
Dim oRef
Dim fso,f
 
strTitle="File Type Search"
strType=InputBox("What type of file do you want to look for? Do NOT use a period.",strTitle,"mp3")
 If strType="" Then 
  wscript.echo "Nothing entered or you cancelled"
  wscript.quit
 End If 
strDrive=InputBox("What local drive do you want to search?  Do NOT use a trailing ",strTitle,"c:")
 If strDrive="" Then
  wscript.echo "Nothing entered or you cancelled"
  wscript.quit
 End If 
 
'trim strDrive just in case the user added a 
strDrive=Left(strDrive,2)
 
strOutput=InputBox("Enter full path and filename for the CSV file.  Existing files will " & _
"be overwritten.",strTitle,"c:\" & strType & "-query.csv")
 If strOutput="" Then
 wscript.echo "Nothing entered or you cancelled"
  wscript.quit
 End If 
 
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
 
wscript.echo "Working ...."
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

Open in new window

Avatar of sydpolo
sydpolo

ASKER

Thanks for that, question on the Query bit
strQuery="Select Name,CreationDate,LastAccessed,LastModified," & _
"FileSize,Extension,Drive FROM CIM_DATAFILE WHERE Extension='" & strType & _
 "' AND Drive='" & strDrive & "'"

how can i search by  specific folder rather than Drive?
ASKER CERTIFIED SOLUTION
Avatar of MeCanHelp
MeCanHelp
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shanmuga Sundaram D
This will get the drive and foldername and the file extension and will search for the mentioned file. The results will be stored in a text file
Dim objFSO
Dim ofolder
Dim objStream
Dim strSearch
Dim strfolderSearch
Set objFSO = CreateObject("scripting.filesystemobject")
Set objStream = objFSO.createtextfile("c:\search.txt", True)
strfolderSearch = inputbox ("Please enter the folderpath with foldername for which you want to search for Eg. C:\Delphi")
strSearch = inputbox ("Please enter the file extension for which you want to search for Eg. .bat")
CheckFolder (objFSO.getfolder(strfolderSearch)), objStream
MsgBox "File Search Completed." + vbCr + "Please check c:\search.txt for details."
 
Sub CheckFolder(objCurrentFolder, objtxtFile)
Dim strTemp
Dim strOutput
Dim objNewFolder
Dim objFile
Dim objStream
For Each objFile In objCurrentFolder.Files
strTemp = Right(objFile.Name, 4)
If UCase(strTemp) = UCase(strSearch) Then
strOutput = CStr(objFile.Name) + ","  + CStr(objFile.Path) + "," + CStr(objFile.Size)  + "," + CStr(objFile.Type) + ","  + CStr(objFile.datelastaccessed)
objtxtFile.writeline strOutput
End If
Next
For Each objNewFolder In objCurrentFolder.subFolders
CheckFolder objNewFolder, objtxtFile
Next
End Sub

Open in new window