By using the following VBSCript one can search for a string in a file.
License : GNU 3 or later
Author : Mayank Johri <johri.maya at gmail.com >
if WScript.Arguments.count <> 3 then
wscript.echo "#Usage: searchcontent.vbs ContentToSearch FolderToSearch ExtOfFile"
else
'Now, we want to search all of the files
Dim fso
'Constant to read
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
'Specify the folder path to search.
Dim FolderToSearch
strtextToSearch = WScript.Arguments.Item(0)
FolderToSearch = WScript.Arguments.Item(1)
fileExt = WScript.Arguments.Item(2)
'Proceed if folder exists
if fso.FolderExists(FolderToSearch) then
Wscript.echo "Searching for " & strtextToSearch & "..."
Dim objFolder
Set objFolder = fso.GetFolder(FolderToSearch)
Dim objFile, objTextStream, strFileContents, bolFileFound
bolFileFound = False
Dim FilesCounter
FilesCounter = 0 'Total files found
Wscript.Echo "Files Found :"
For Each objFile in objFolder.Files
'wscript.echo fso.GetExtensionName(objFile.path) & ", " & fileExt
if fso.GetExtensionName(objFile.path) = fileExt then
' Wscript.echo "Searching in file " & objFile.path
'if objFile.GetExtension = fileExt(
Set objTextStream = fso.OpenTextFile(objFile.Path,ForReading)
'Read the content
strFileContents = objTextStream.ReadAll
If InStr(1,strFileContents,strtextToSearch,1) then
Wscript.Echo objFile.path '& "\" & objFile.Name
FilesCounter = FilesCounter + 1
End If
objTextStream.Close
end if
Next
Set sf = objFolder.SubFolders
For Each f1 in sf
' wscript.echo f1.name
searchfolder f1.path , strtextToSearch
Next
if FilesCounter = 0 then
Wscript.Echo "Sorry, No matches found."
else
Wscript.Echo "Total files found : " & FilesCounter
end if
'Destroy the objects
Set objTextStream = Nothing
Set objFolder = Nothing
else
Wscript.Echo "Sorry, invalid folder name"
end if
Set fso = Nothing
function searchfolder (folderToSearch, txtToSearch)
Set objFolder = fso.GetFolder(FolderToSearch)
For Each objFile in objFolder.Files
'wscript.echo fso.GetExtensionName(objFile.path) & ", " & fileExt
if fso.GetExtensionName(objFile.path) = fileExt then
'Wscript.echo "Searching in file " & objFile.path
'if objFile.GetExtension = fileExt(
Set objTextStream = fso.OpenTextFile(objFile.Path,ForReading)
'Read the content
strFileContents = objTextStream.ReadAll
If InStr(1,strFileContents,strtextToSearch,1) then
Wscript.Echo objFile.path ' & "\" & objFile.Name
FilesCounter = FilesCounter + 1
End If
objTextStream.Close
end if
Next
Set sf = objFolder.SubFolders
For Each f1 in sf
'wscript.echo f1.path
searchfolder f1.path, txtToSearch
Next
end function
end if
No comments:
Post a Comment