How to Use the Find Command to Search Text Strings in Files
The Find command searches for text strings in file or files and returns the lines of text from the file or files in which the text string was found.
Please open the Command Prompt by clicking Start Menu --> All Programs --> Accessories, right-click on Command Prompt, and open it as an administrator. In Windows Vista and later versions of Window operating systems type cmd.exe into Start Screen or Start Menu, right-click on Cmd.exe, and open it as an administrator.
Syntax for the Find Command
A command’s syntax is a specific way to organize the command and its parameters. The following is the general syntax for the Find command.
For the “string” parameter, you must surround the string with double quotes, otherwise the find command will not work and will return an error.
You can view the help document for the Find command by typing the following command at the Command Prompt and pressing Enter key.
The brackets [] indicate that the parameter is optional.
How to Search a Single File for a Text String
The following command searches the example.txt file for the phrase “GeekPolice is the best forums”.
NOTE: If there are spaces in any part of the path or file name, you must put quotes around the entire path, like we did in the above command. The quotes are not really needed in this case, but it doesn’t hurt to have them.
Now that you can search one file for a text string, let’s search multiple files for the same text string.
You can specify multiple files to search in the Find command by putting the path to each file in quotes separated by a space.
You can use wildcard character, which is an asterisk (*), to search all text files in a directory, as shown in following command.
The search term was found in both documents and the sentences in which they were found are listed under the full path to each file.
NOTE: The Find command is not suitable for large files or large numbers of files.
You can use the Find command with the For command for large numbers of files or folders. To search the current directory for files that have the extension .bat and that contain the string "PROMPT," type:
You can search your hard disk to find and display the file names on drive C: that contain the string "CPU".
Because Find searches are case-sensitive, you must either type the string "CPU" in uppercase letters or use the /i parameter with Find, which ignores the case of characters when searching for the string.
If you want to know how many lines there are in a file, you can use a combination of the Type and Find commands. The Type command displays the contents of one or more text files.
The vertical bar (|) pipes (outputs) the results of the Type command into the Find command. The “/v” parameter displays all lines NOT containing the “” string, so every line with text will be counted. To display only the number of lines in the text file (not the lines themselves), we use the “/c” switch.
Please open the Command Prompt by clicking Start Menu --> All Programs --> Accessories, right-click on Command Prompt, and open it as an administrator. In Windows Vista and later versions of Window operating systems type cmd.exe into Start Screen or Start Menu, right-click on Cmd.exe, and open it as an administrator.
Syntax for the Find Command
A command’s syntax is a specific way to organize the command and its parameters. The following is the general syntax for the Find command.
Code:
Find [Parameters] "string" [[Drive:][Path]FileName[...]]
For the “string” parameter, you must surround the string with double quotes, otherwise the find command will not work and will return an error.
You can view the help document for the Find command by typing the following command at the Command Prompt and pressing Enter key.
Code:
Find /?
The brackets [] indicate that the parameter is optional.
How to Search a Single File for a Text String
The following command searches the example.txt file for the phrase “GeekPolice is the best forums”.
Code:
Find /i "GeekPolice is the best forums" "C:\example.txt"
NOTE: If there are spaces in any part of the path or file name, you must put quotes around the entire path, like we did in the above command. The quotes are not really needed in this case, but it doesn’t hurt to have them.
Now that you can search one file for a text string, let’s search multiple files for the same text string.
You can specify multiple files to search in the Find command by putting the path to each file in quotes separated by a space.
Code:
Find /i "GeekPolice is the best forums" "C:\example.txt" "C:\example2.txt"
You can use wildcard character, which is an asterisk (*), to search all text files in a directory, as shown in following command.
Code:
Find /i "GeekPolice is the best forums" "C:\*.txt"
The search term was found in both documents and the sentences in which they were found are listed under the full path to each file.
NOTE: The Find command is not suitable for large files or large numbers of files.
You can use the Find command with the For command for large numbers of files or folders. To search the current directory for files that have the extension .bat and that contain the string "PROMPT," type:
Code:
for %f in (*.bat) do find /i "PROMPT" %f
You can search your hard disk to find and display the file names on drive C: that contain the string "CPU".
Code:
dir c:\ /s /b | find "CPU"
Because Find searches are case-sensitive, you must either type the string "CPU" in uppercase letters or use the /i parameter with Find, which ignores the case of characters when searching for the string.
If you want to know how many lines there are in a file, you can use a combination of the Type and Find commands. The Type command displays the contents of one or more text files.
The vertical bar (|) pipes (outputs) the results of the Type command into the Find command. The “/v” parameter displays all lines NOT containing the “” string, so every line with text will be counted. To display only the number of lines in the text file (not the lines themselves), we use the “/c” switch.
Code:
Type "E:\My_Notes\Example.txt" | Find "" /v /c