user2428207
user2428207

Reputation: 825

How do I find a string in text files and print the matches in a Windows batch file?

Is there a way to search for a defined string within several thousand text files and to print the names of the files that have a match?

Upvotes: 2

Views: 1423

Answers (2)

Aacini
Aacini

Reputation: 67216

findstr /M "searched string" *.txt > matchingFiles.out

From findstr /? documentation:

/M         Prints only the filename if a file contains a match.

Upvotes: 3

Rajesh Balan
Rajesh Balan

Reputation: 643

Not sure which OS platform to be answered for,

But Linux/Unix systems can have the following command used for this purpose.

find . -name '*.ext' -type f | xargs fgrep 'pattern to be searched'

This will search the files starting from current directory and check for the 'pattern to be searched'. The file name pattern with wildcard can be applied where I have used '*.ext'

Upvotes: 0

Related Questions