We have discussed finding files in the current and specified directories using DIR
command from Windows command prompt. DIR
command doesn’t useful to search the stringĀ in the files. Hence we have FIND
command.
FIND
command
This command is used to search for a string in the files. You need to pass relevant arguments to this command; otherwise, Windows will display below message.
FIND: Parameter format not correct
Basic parameters you need to pass; a string to search and the path of the file. The string should be in double quotes; otherwise, you will see above message. For example, below command search for the string “hello” in the file “sample.txt“.
C:\>find sample.txt "hello" ---------- SAMPLE.TXT hello, CodeSteps!
Observe that, you see the entire line from the file where it has “hello” text. The search is case sensitive.
To ignore the case, you need to pass a parameter “/I” to it. Below is the example (it treats both lowercase and uppercase letters are the same).
C:\>find "codesteps" sample.txt /i ---------- SAMPLE.TXT hello, CodeSteps!
By using “/V” parameter it displays the lines of text where the given string NOT found.
Display the line number
By using the parameter “/N” it will display the line number where it finds the given text and the line of text.
C:\>find "Steps" sample.txt /n ---------- SAMPLE.TXT [3]hello, CodeSteps!
Display count of lines where it finds the text
FIND
command provides a count value of number of lines where the given text is found in the file(s). It needs “/C” as the parameter to display the count. For example, below command shows the count of lines where the given text “task” is found.
C:\>find "task" sample.txt /c ---------- SAMPLE.TXT: 4
Find text in multiple files
FIND
command allows to search the given string in multiple files. You need to pass each file separately or you can use wildcards to select multiple files. For example, below command finds the text in multiple files.
C:\>find "task" tasks.txt notes.txt address.txt
When you use wildcards, the syntax will be easier. Below command search for the string in all “.txt” files.
C:\> find "true" *.txt
Keep in mind that FIND
command is used to search for a single string ONLY.
Be on learning path always!
// Malin