Windows recursive grep command-line - windows

I need to do a recursive grep in Windows, something like this in Unix/Linux:
grep -i 'string' `find . -print`
or the more-preferred method:
find . -print | xargs grep -i 'string'
I'm stuck with just cmd.exe, so I only have Windows built-in commands. I can't install Cygwin, or any 3rd party tools like UnxUtils on this server unfortunately. I'm not even sure I can install PowerShell. Any suggestions using only cmd.exe built-ins (Windows 2003 Server)?

findstr can do recursive searches (/S) and supports some variant of regex syntax (/R).
C:\>findstr /?
Searches for strings in files.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurrences of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.

findstr /spin /c:"string" [files]
The parameters have the following meanings:
s = recursive
p = skip non-printable characters
i = case insensitive
n = print line numbers
And the string to search for is the bit you put in quotes after /c:

I just searched a text with following command which listed me all the file names containing my specified 'search text'.
C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*

Recursive search for import word inside src folder:
> findstr /s import .\src\*

I recommend a really great tool:
native unix utils:
http://unxutils.sourceforge.net/
http://en.wikipedia.org/wiki/UnxUtils
Just unpack them and put that folder into your PATH environment variable and voila! :)
Works like a charm, and there are much more then just grep ;)

for /f %G in ('dir *.cpp *.h /s/b') do ( find /i "what you search" "%G") >> out_file.txt

Select-String worked best for me. All the other options listed here, such as findstr, didn't work with large files.
Here's an example:
select-string -pattern "<pattern>" -path "<path>"
note: This requires Powershell

If you have Perl installed, you could use ack, available at http://beyondgrep.com/.

"findstr /spin /c:"string" [[drive:][path]filename[...]]"
Similar to the 2nd highest answer above (by i_am_jorf on Mar 30, 2009 at 22:26) which shows the following example: "findstr /spin /c:"string" [files]"
However, running "findstr /?" shows there is no option or parameter defined as
"[files]". I believe what he is implying here is the parameter that defines which files to search for which "findstr /?" describes as:
"[[drive:][path]filename[ ...]]"
It later defines this with the following:
"[drive:][path]filename" - Specifies a file or files to search.
So, to not use personal short-hand I am providing it the way that findstr /> defines it if searching for certain files:
"findstr /spin /c:"string" [[drive:][path]filename[...]]"

Related

How to use OR operator with command FINDSTR from a Windows command prompt?

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. Windows thinks on using | that I try to pipe the previous command and OR is read as literal OR.
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 Eryk Sun explained in his comment above, 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 returns 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 one 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
A brief description between the differences on using "..." or /C:"...":
"regexp1 regexp2 regexp3" means searching for a line with a string matched by one of the three space separated regular expressions. The option /R can be used additionally to explicitly interpret the three strings between the two spaces as regular expressions. It is advisable to do so for making it 100% clear for findstr and every reader that the search strings are interpreted as regular expressions.
/L "word1 word2 word3" means searching for a line with a string matched by one of the three space separated literally interpreted strings. The used option /L forces explicitly an interpretation of the three strings between the two spaces as literal strings and not as regular expressions.
/C:"word 1" /C:"word 2" /C:"word 3" means searching for a line with a string matched by one of the three literally interpreted strings on which the space character is interpreted as space. The option /L can be used additionally to explicitly interpret the three search strings between as literal strings. It is advisable to do so for making it 100% clear for findstr and every reader that the search strings are interpreted as literal strings.
/R /C:"reg exp 1" /C:"reg exp 2" /C:"reg exp 3" means searching for a line with a string matched by one of the three regular expressions strings on which the space character is interpreted as space. The option /R forces explicitly an interpretation of the three strings as regular expressions with space being interpreted as space.

Windows Findstr

I'm trying to find files in a folder with specific pattern like:
abcd201 abcd001 abcd004
The folder contains files named
abcd(3 numbers)
I'm trying to use the pattern:
abcd[0,2][0][1,4] but currently not working.
DIR /b C:\Folder\abcd"[0,2][0][1,4]".txt
Thanks!
dir command does not support regular expressions. You need to filter the output with findstr
dir /b "c:\folder\abcd*.txt" | findstr /r /c:"^abcd[02]0[14]\.txt$"
That is, use dir command to obtain a first approximation of what you are searching and then filter the list (pipe the dir command to findstr) to obtain only the list of required files.
The regular expression (/r) in findstr means: filter the lines, starting at the start of the line (initial ^), followed by abcd, followed by any character in the set [02], followed by a 0, followed by any character in the set [14], followed by a dot (a single dot means any character, so, it needs to be escaped \.), followed by the string txt and the end of the line ($).
Maybe you will need to add a /i switch to findstr to indicate it must ignore case when matching.
The regex of your example would also match abcd204 name. You may find these 4 files in a simpler way:
for %a in (0 2) do for %c in (1 4) do dir /B C:\Folder\abcd%a0%c.txt 2>NUL
This method is faster than findstr's one, especially if the number of files is large.

want to copy names of files to text in a directory based on their file extension

I want to copy names of files to text in a directory based on their file extension.
As of now I am using dir /b >i67.txt which works fine for me but its not resolving problem of specific file extensions.
Can someone help me in getting a batch script for the same.
You are looking for the following command, run it in the context of the directory which contains your files:
dir /b /s /-p *.txt /o:n | findstr /E .txt > i67.txt
Using the above code example, you will be able to find all *.txt files in the directory and output the results into the i67.txt file (will be outputted to the same directory).
You can specify multiple file masks within one DIR /B command. Based on your comment to Yair Nevet's answer, it seems you want the following extensions: .ovr, .inc, and .dat. That can be done simply using:
dir /b /s *.ovr *.inc *.dat >i67.txt
If the files are on an NTFS volume that has short 8.3 names enabled, then you might get additional undesired file extensions if you have any file extensions longer than 4 characters that begin with your wanted extension. For example someName.data would show up in your output because it most likely would have a short name of SOMENA~1.DAT that matches your file mask.
You can prevent short name inclusion by piping the output to FINDSTR. The /L option forces a literal search as opposed to regular expressions, the /I option ignores case, and the /E option matches only the end of each line. Multiple search terms are delimited by spaces.
dir /b /s *.ovr *.inc *.dat | findstr /lie ".ovr .inc .dat"
Regarding your following comment:
Here is what I am using now: dir /b | findstr [a-z].*ovr>i67.txt &&
dir /b | findstr [a-z].*inc>>i67.txt && dir /b | findstr
[a-z].*dat>>i67.txt What it does?? --- It copies all
names(remember,only name except files itself which are ending with
extension .ovr .dat and .cpi ) present in a directory and copy it to a
text file(here name is i67.txt)
That will not actually do what you want for several reasons.
Windows file names are not case sensitive. Windows would treat NAME.OVR and name.ovr the same, so you should as well. That requires the /I option.
There is nothing in your search to anchor ovr to the extension. It will look for your pattern anywhere within the file name. And the dot is a meta character that represents any character - not a literal dot. The asterisk allows the dot to match any number of characters.
I can't be sure, but it looks like perhaps you only want to match files that begin with a letter. The following modification to my answer should do the trick:
dir /b /s *.ovr *.inc *.dat | findstr /ri "^[a-z].*\.ovr$ ^[a-z].*\.inc$ ^[a-z].*\.dat$"
The \R option forces a regular expression match instead of a literal. It is the default behavior for the given search, but it is a good idea to be explicit with regard to regex vs literal search.
^ anchors the search to the beginning of the name
[a-z] matches any letter (sort of). Remember it is not case sensitive because of the /I option. Without the /I option, it would not match upper case Z. See Why does findstr not handle case properly (in some circumstances)? for an explanation.
.* matches any number of characters, without restriction
\. matches a dot literal, marking the beginning of your extension
Then comes your extension
$ anchors the match to the end of the name

findstr.exe is not working

My system has findstr.exe but when I try to execute it, it gives me the following error
FINDSTR: Bad command line
Tried so many things but unable to fix.
I need to use regex in my batch script.
Any other suggestion?
You need to at least give it some strings to look for. That error message is the one you get if it doesn't think you've provided a search string (everything else is optional):
C:\Documents and Settings\Pax> findstr /?
Searches for strings in files.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurances of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\ Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.
For example, this shows how you can use regular expressions:
C:\Documents and Settings\Pax> type qq.cmd
#setlocal enableextensions enabledelayedexpansion
#echo off
set startdir=%cd%
set temp=%startdir%
set folder=
:loop
if not "x%temp:~-1%"=="x/" (
set folder=!temp:~-1!!folder!
set temp=!temp:~1,-1!
goto :loop
)
echo.startdir = %startdir%
echo.folder = %folder%
endlocal
C:\Documents and Settings\Pax> findstr d.r% qq.cmd
set temp=%startdir%
echo.startdir = %startdir%
echo.folder = %folder%
C:\Documents and Settings\Pax> findstr
FINDSTR: Bad command line
For anyone else who is struggling with this, try this simple syntax for a start:
findstr /s /i hello *.*
(ignore case, search all subdirectories in current folder)
Of course, you didn't specify any command after FINDSTR command. Type FINDSTR /? for help.
This an example how to use FINDSTR command:
FINDSTR /R /C:"your_regex" filename.txt
That's what findstr says when you give it no command line arguments. Try
findstr/?

What is the closest thing to grep that comes standard on a Windows install?

I'd like to do something like "dsquery * | grep asdf" on a Windows machine that I can't install anything on. Any ideas?
Thank you.
findstr:
dsquery * | findstr "asdf"
The findstr command is what you're looking for. It's a little different than grep, but you can do some of the same things.
C:\Working>findstr /?
Searches for strings in files.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurrences of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\ Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.
dsquery * | find "asdf"
"find" appears since DOS ages.
"findstr" is newer and feature richer than "find"

Resources