Reputation: 151
Is it possible to use the find function to search a folder and return the names of files that have any of a collection of strings
For example, search a folder and return and files with the text 'Michael', 'Alan', 'Ben' etc.
Upvotes: 1
Views: 4459
Reputation: 130849
I didn't think DOS had FINDSTR, but if you do have it, then you can use the following to find the names of files whose contents contain "fox" or "dog"
findstr /ml "fox dog" "myDir\*"
or
findstr /m /c:"fox" /c:"dog"
But there is a nasty FINDSTR bug that can result in missing files that should match when you have multiple literal search strings of different lengths and the search is case sensitive. Since "dog" and "fox" are the same length, you won't have a problem. But I suspect your real search strings will vary in length. Hopefully you can get away with a case insensitive search, because that avoids the bug:
findstr /mli "string1 string2IsLonger" "myDir\*"
or
findstr /mi /c:"string1" /c:"string2IsLonger"
See Why doesn't this FINDSTR example with multiple literal search strings find a match? for more info about the bug. I also recommend reading What are the undocumented features and limitations of the Windows FINDSTR command?.
If any of your search strings contain spaces, then you will need to use the /c
option.
If you have to search for many search strings, then I suggest you use the /g:file
option.
You can type HELP FINDSTR
or FINDSTR /?
from the command line to get a complete list of available options.
Upvotes: 0
Reputation: 65516
you can use the Findstr command DOS and Windows.
FINDSTR [options] [/F:file] [/C:string] [/G:file]
[/D:DirList] [/A:color] [/OFF[LINE]] [string(s)] [pathname(s)]
Literal search
Search a text file mydir\*.* that contains the following
The quick brown fox The really ^brown^ fox
A literal search will ignore any special meaning for the search characters:
FINDSTR /C:"^brown" mydir\*.*
Upvotes: 2