[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15429
  • Last Modified:

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.

0
sydpolo
Asked:
sydpolo
1 Solution
 
chandru_solCommented:
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
 
sydpoloAuthor Commented:
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
 
MeCanHelpCommented:
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
 
Shanmuga SundaramCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now