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/?
Related
for /d %%A IN (u:\mainfolder\*) DO if not exist "%%A\%var1%" mkdir "%%A\subfolder"
I want to exclude certain folders in the directory u:\mainfolder\ so that mkdir executes on all but a few of the folders there. Is there a way to exclude certain folders given the use of the wildcard?
If you only have 2 or 3 to exclude you can do something like this.
for /d %%A IN (u:\mainfolder\*) DO if /i not "%%A"=="u:\mainfolder\Name1ToExclude" if /i not "%%A"=="u:\mainfolder\Name2ToExclude" if not exist "%%A\%var1%" mkdir "%%A\subfolder"
Otherwise if you have more, you probably want to use a table of names to exclude.
The command FOR does not support an exclude option.
But the task can be achieved using the commands DIR and FINDSTR executed by FOR.
#echo off
set "MainFolder=U:\mainfolder"
for /F "delims=" %%A in ('dir "%MainFolder%\*" /AD /B ^| %SystemRoot%\System32\findstr.exe /E /I /L /V /X /C:"Exclude Folder 1" /C:ExcludeFolder2 /C:FolderToExclude3 2^>nul') do if not exist "%MainFolder%\%%A\%var1%" mkdir "%MainFolder%\%%A\subfolder"
set "MainFolder="
The command DIR is executed to output only directories in directory specified with environment variable MainFolder because of the options /AD (attribute directory) and /B (bare format). The directory names are output by DIR without path, just the directory names.
The output of DIR is piped as input to FINDSTR using redirection operator |.
FINDSTR searches in all lines for one of the strings specified with option /C as literal string because of option /L not enclosed in double quotes or enclosed in double quotes because of directory name contains a space character or one of these characters: &()[]{}^=;!'+,`~
The search is case-insensitive because of option /I.
A match is only positive if the enter line matches completely with a search string because of /X which means the entire directory name must match completely with one of the search strings.
The option /V results in an inverted output by FINDSTR. Instead of printing the lines matching with one of the search strings, it prints the lines (= directory names) not matching with any of the search strings.
The filtered directory names without path are processed line by line by FOR.
The redirection operators | and > must be escaped with caret character ^ in the finally executed command line:
dir "U:\mainfolder\*" /AD /B | C:\Windows\System32\findstr.exe /E /I /L /V /X /C:"Exclude Folder 1" /C:ExcludeFolder2 /C:FolderToExclude3 2>nul
This command line is executed by FOR with using a separate command process started in the background. The redirection operators | and > must be escaped with ^ to be interpreted as literal characters on parsing the entire FOR command line by Windows command interpreter before executing command FOR with the rest of the line.
On a longer list of directories to exclude I suggest to write the directory names into a plain text file and use option /G: of FINDSTR instead of specifying them all with /C: on command line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
findstr /?
for /?
set /?
Read also the Microsoft article about Using Command Redirection Operators.
I need to find the files in a directory that have specific strings, using Windows CMD prompt.
E.g., I need to find the files that have a string like this:
<h1>Select an Item</h1>
"findstr" iswhat you are looking for.
findstr /I "<h1>Select\ an\ Item</h1>" *.*
findstr is the command, /I is a flag to match the string case insensitive. "<h1>Select\ an\ Item</h1>" is your string (note the escaped spaces!) and *.* means "in all files in this directory".
The basic syntax is findstr "seachString" filename.ext.
You may replace filename.ext with *.ext or *.* to filter cretin file types or look in all files.
This will look only in the current directory and not recursively.
More information about the command findstr documentation
The command you require is fundamentally findstr.
type
findstr /?
at the prompt for directions.
The command that may work for you is
findstr /m /g:"a file containing your string or strings" *
or
findstr /m /L /c:"<h1>Select an Item</h1>" *
Where some experimentation with the contents of the "quoted string" may be required, especially wrt characters line <>() and others with a particular meaning to cmd.exe.
Find some string inside all text files in current directory:
cls & for %i in (*.txt) do find /i "search text" < "%i" && (echo : %i & echo -)
Tested in Win 10
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.
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[...]]"
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"