?
Solved

VBSCRIPT search for file type

Posted on 2007-12-02
6
Medium Priority
?
15,311 Views
Last Modified: 2010-07-15
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.

0
Comment
Question by:sydpolo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20390707
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

0
 

Author Comment

by:sydpolo
ID: 20399120
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?
0
 
LVL 4

Accepted Solution

by:
MeCanHelp earned 2000 total points
ID: 20403095
This one is another way of doing it: You will have to change the Jade part to the file path you want and of course the extension from OCX to what ever but this will rip through a folder and write to file the extensions you want to.

strComputer = "."
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\report.txt", ForAppending, True)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService. _
    ExecQuery("Select * from CIM_DataFile where Path = '\\Jade\\'")

For Each objFile in colFiles
   If objfile.extension = "ocx" Then
      objTextFile.WriteLine(objfile.name)
   End If
Next
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20937517
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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question