Program to search for all files that have a designated text string or strings. Windows Explorer has a search feature for this purpose. I have used it for years and depend on it. However, the feature does not work as expected in the new operating systems. On Windows XP, Vista, and Windows 7, ans above, Windows Explorer "seems" to search for files that contain a specified text string. Some files may be found and there are no error or warning messages. But not all files that contain the text string are found. In fact most files are not found.

Research indicates an indexing service must be configured to enable this search functionality in Windows Explorer. I assume this would be at the cost of performance. I have not taken the time to learn how to configure the indexing service. I would need to configure the service on every computer where I might search. You must specify in advance which file extensions will be indexed. I'm not sure how this would work in a network environment.

This program uses the FileSystemObject in a recursive subroutine to enumerate all files in the specified path. Each file is opened with a TextStream object and searched for the designated string or strings. This does not seem like it would be an efficient method. However, it is dependable and works. If the program reports that no files are found with the specified text string(s), you can be sure that no files have been missed.

The syntax to run the program at a command prompt would be:

cscript FindFiles.vbs <string> <path> <extension> <searchSubs> <caseSensitive>

<string> is the string or strings to be searched for. This is the only required parameter. Enclose any string in quotes if it contains spaces. All strings are case insensitive. No string can contain embedded quote characters. You can specify up to three strings, delimited by the semicolon character, ";". If any string has an embedded semicolon, escape it with the backslash, "\". The program is not designed to find files that contain any one of several strings. Each file output by the program will contain all of the specified strings. If more than three strings are specified the program aborts.

<path> is the optional path to be searched. Enclose in quotes if it contains spaces. The current path is used if no path is specified. You can also specify "." for the current directory and ".." for the parent of the current directory.

<extension> is an optional comma delimited list of file extensions to be searched, with no spaces. Use "." to indicate all extensions, which is the default.

<searchSubs> is an optional Boolean to indicate if all sub folders are to be searched. "T" or "True" means to search all sub folders. "F" or "False" means to only search the specified folder. The default is "False".

<caseSensitive> is an optional Boolean to indicate if the search is case sensitive. "T" or "True" means the search is case sensitive (for all strings specified). "F" or "False" means the search is case insensitive, which is the default.

The program outputs the name and path of every file found to contain all of the specified string(s), one file per line. The program also displays the file last modified date and size in bytes in parentheses. Then the program outputs the number of files found. You can redirect the output to a text file. Examples of the syntax follow:

cscript FindFiles.vbs WinNT:;LDAP: "c:\My Folder\Scripts" vbs true true
cscript FindFiles.vbs "My Server" . vbs,txt,csv f f
cscript FindFiles.vbs NameTranslate c:\Scripts . t
cscript FindFiles.vbs "Test \; 2";Test3\;;Test4 c:\Scripts vbs

Since this program was written, I have discovered the FindStr command. This new utility can also be used to find files that contain a specified string. However, my testing indicates it is no faster than this VBScript program.

The output from the program might look similar to below:

cscript //nologo FindFiles.vbs ADODB;NameTranslate c:\Scripts vbs,txt t t
C:\Scripts\TestProgram1.vbs      (8/22/2009 2:23:12 PM 12,466)
C:\Scripts\Example.vbs   (10/3/2008 9:03:12 AM 743)
C:\Scripts\Test\Simple.txt       (2/13/2009 9:32:32 AM 1,203)
C:\Scripts\West\FindUsers.vbs    (4/17/2009 3:04:21 PM 4,398)
4 files found with string ADODB;NameTranslate

For the last several years I have used a batch file to run the VBScript program. The batch file accepts up to 5 parameters, which are passed to FindFiles.vbs. I name the batch file f.bat and save it in a folder on the path. The batch file is similar to the following:

@echo off
cscript //nologo c:\Scripts\FindFiles.vbs %1 %2 %3 %4 %5

I use the batch file f.bat to search files for strings at a command prompt similar to the following:

f "ldp;LDAP" . htm t t

If I am searching for more than one string with the batch file, the strings must be enclosed in double quotes to get passed properly to the VBScript program. The same applies if I specify more than one extension.

FindFiles.txt <<-- Click here to view or download the program