How to use OR operator in findstr command from a windows 7 command prompt?

不羁的心 提交于 2019-12-10 15:16:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!