search for a string in files in folder


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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s