Solved

VBSCRIPT search for file type

Posted on 2007-12-02
6
15,156 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 4 52
oracle query help 18 74
C# code editing and collaboration 3 39
how to update exe applicatio from internet ? 6 34
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 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

12 Experts available now in Live!

Get 1:1 Help Now