The command: "FINDSTR" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available
The examples for the command "FINDSTR"
The
`FINDSTR` command in the Windows Command Prompt is a more powerful alternative to
`FIND`. It offers advanced features and supports regular expressions. Here are some examples:
Example 1: Simple text search:
FINDSTR "Search term" file.txt
Description: Searches for the text
"Search term" in the file
"File.txt" and prints all lines that contain the string.
Example 2: Ignore case sensitivity:
FINDSTR /I "search term" file.txt
Description: Ignores case sensitivity when searching for
"search_term" in the file
"File.txt".
Example 3: Using Regular Expressions:
FINDSTR /R "^Start of line" file.txt
Description: Searches for lines in
"File.txt" that start with
"start of line" using regular expressions (
`/R`).
Example 4: Search for a string and its surroundings:
FINDSTR /C:"Search term" /C:"OtherString" /B /E File.txt
Description: Searches for the strings
"SearchKey" or
"OtherString" at the beginning (/B) or end (/E) of lines in
"File.txt".
Example 5: Search for multiple strings using regular expressions:
FINDSTR /R "Pattern1.*Pattern2" file.txt
Description: Searches for lines in
"File.txt" that contain both
"Pattern1" and
"Pattern2".
Example 6: Search for a string in all subdirectories:
FINDSTR /S "Search term" C:\Directory\*
Description: Searches for
"search term" in all files in the specified directory and its subdirectories.
Example 7: Searching for a string and displaying the line number:
FINDSTR /N "Search term" file.txt
Description: Searches for
"search term" in
"File.txt" and displays the line numbers of the lines found.
Example 8: Excluding certain character strings:
FINDSTR /V "ExcludedString" file.txt
Description: Displays only the lines in File.txt that do not contain the string ExcludedString.
Example 9: Search for a string with a specific character length:
FINDSTR /R "^.{5}$" file.txt
Description: Searches for lines in
"File.txt" with exactly five characters.
Example 10: Displaying rows with matches and surroundings:
FINDSTR /C:"Search term" /B /E /A:3 File.txt
Description: Searches for the string
"Search Term" at the beginning (/B) or end (/E) of lines in
"File.txt" and additionally displays three lines of surroundings (before and after the match).
It is important to note that
`FINDSTR` offers more possibilities than
`FIND`, especially the use of regular expressions. The exact options can be accessed by
`FINDSTR /?` in the command prompt.
"FINDSTR" Excerpt from Microsoft Windows Help
Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.
C:\\WINDOWS>
Searches for strings in files.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurances of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.
Important information, tips for the "FINDSTR" command
There are a few important points to note when using the
`FINDSTR` command in the Windows Command Prompt:
1.
Case Sensitivity: By default, the search of
`FINDSTR` is case-sensitive, meaning it is case sensitive. If you want to perform a case-insensitive search, add the
`/I` option.
FINDSTR /I "search term" file.txt
2.
Regular Expressions: `FINDSTR` supports the use of regular expressions (
`/R`), allowing advanced search capabilities. You can use regular expressions to create more complex search patterns.
FINDSTR /R "Pattern.*Search term" file.txt
3.
Numerous Options: `FINDSTR` has many options and switches to control the search. These include options for displaying line numbers (
`/N`), ignoring case sensitivity (
`/I`), searching for whole words (
`/W`), displaying the number of lines found (
` /C`), and many more.
FINDSTR /N /I /W "Search term" file.txt
4.
File Types and Binaries: `FINDSTR` defaults to searching text files. If you are looking for binary files or want to search specific file types, you should use the
`/P` option.
FINDSTR /P /I "Search term" binaryfile.bin
5.
Environmental Variables: You can use
`FINDSTR` in conjunction with environment variables to perform dynamic searches. For example:
SET search term=example
FINDSTR /I "%search term%" file.txt
6.
Multiple Searches: You can use
`FINDSTR` to search for multiple strings at once by specifying multiple
`/C:"search_string"` options.
FINDSTR /C:"Term1" /C:"Term2" File.txt
7.
Redirecting output: Like
`FIND`, you can redirect the output of
`FINDSTR` to a file.
FINDSTR "Search term" File.txt > FoundLines.txt
8.
Exit Code: Similar to
`FIND`,
`FINDSTR` returns an exit code. An exit code of 0 means the search term was found, while an exit code of 1 means the search term was not found.
FINDSTR "Search term" file.txt
IF ERRORLEVEL 1 (
ECHO The search term was not found.
) ELSE (
ECHO The search term was found.
)
It is important to consider the extensive options and features of
`FINDSTR` and ensure that you use the appropriate options for your specific search needs. You can get the full list of options and help by typing
`FINDSTR /?` in the command prompt.