问题
Findstr is supposed to support regular expressions and the way I am using it I need to have an 'or' to check if a file ends in ".exe" or ".dll". However I cannot get the or operation to work. When using '|' windows thinks I am trying to pipe the previous command and 'OR' is read as literally OR
回答1:
findstr.exe
in Windows system32 directory supports only a very limited set of regular expression characters. Running in a command prompt window findstr /?
results in getting displayed help for this console application listing also the supported regular expression characters with their meanings.
But as eryksun explained in his comment, multiple search strings can be specified on command line to build a simple OR expression.
In case of having a list file FileNames.lst
containing for example
C:\Program Files\Internet Explorer\ieproxy.dll
C:\Program Files\Internet Explorer\iexplore.exe
C:\Program Files\Internet Explorer\iexplore.exe.mui
and just all file names ending with .dll
OR .exe
case-insensitive should be output by command findstr, the command line for getting this output could be:
%SystemRoot%\system32\findstr.exe /I /R "\.exe$ \.dll$" FileNames.lst
The output is for the example lines in FileNames.lst
:
C:\Program Files\Internet Explorer\ieproxy.dll
C:\Program Files\Internet Explorer\iexplore.exe
The space in regular expression search string is interpreted by findstr as a separator between the two strings. Therefore findstr searches with the regular expression strings \.dll$
and \.exe$
and return all lines where one of the two expressions matches a string.
Another method to OR two or more regular expression strings would be using parameter /C:"..."
multiple times on command line which is necessary when a regular expression search string contains 1 or more spaces which should be included as literal character(s) in search expression.
%SystemRoot%\system32\findstr.exe /I /R /C:"\.dll$" /C:"\.exe$" FileNames.lst
The result is the same as above with the other command line.
But for this specific task it is not necessary at all to run a regular expression search as findstr offers also the parameter /E
for returning only lines where the searched strings are found at end of a line.
%SystemRoot%\system32\findstr.exe /E /I /C:.exe /C:.dll FileNames.lst
来源:https://stackoverflow.com/questions/29103843/how-to-use-or-operator-in-findstr-command-from-a-windows-7-command-prompt