Solved

VBSCRIPT search for file type

Posted on 2007-12-02
6
15,184 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now