Use Batch findstr for files with whitespaces
Solution 1
All you need is the following:
findstr /m /c:toFind *
It will search every file in your current directory, and print out the names of files that contain your search string.
If you want to search a folder other than the current directory, then:
pushd "c:\yourPath"
findstr /m /c:toFind *
popd
If you want to include the full path of each file in the output, then:
findstr /m /c:toFind "c:\yourPath\*"
The proper way to use a FOR loop to iterate files like you originally intended is:
for %%F in (*) do findstr /m /c:toFind "%%F"
But there is no need to use FOR to iterate the files when FINDSTR can iterate the files on its own.
The FOR command is the most complicated command available to batch. There are many different forms that do completely different things. Here are just a few:
for %%A in (file list with wildcard support) - iterate files
for /f "options" %%A in (filePath) - read and parse a file
for /f "options" %%A in ("string") - parse a string
for /f "options" %%A in ('someCommand') - parse the output of a command
You should read the documentation very carefully. Type HELP FOR
or FOR /?
from the command prompt to get the documentation.
Solution 2
I found the solution.
It seems to work with double quotes on the in
clause and with an /F
instead of /R/ for the
for`:
@echo off
for /f %%i in ("*") do (
findstr /m /C:toFind "%%i"
)
Related videos on Youtube
Alvaro
Web developer. Mainly working with PHP (and CakePHP framework), CSS and jQuery.
Updated on September 18, 2022Comments
-
Alvaro over 1 year
I'm trying to search for a string in a given file. This file can have whitespaces in its name and I'm having trouble with the Batch file.
I can easily do this by console getting the correct results:
findstr /m /C:tofind "C:/myPath/my file name with white spaces"
But when I try to do it on the Batch file it doesn't show me any result at all:
@echo off for /r "delims:|" %%i in (*) do ( findstr /m /C:tofind "%%i" )
What am I doing wrong?
-
Marcks Thomas over 10 yearsThe bash file you speak of is a typo, I assume. What exactly is the purpose of the for loop? The command
for /r
does not take options likefor /f
, for which the syntax would bedelims=
instead ofdelims:
. -
Alvaro over 10 years@MarcksThomas I just want to iterate over the elements of the folder. Don't know other way to do it.
-
Alvaro over 10 yearsI took it from: stackoverflow.com/questions/138497/…
-
Alvaro over 10 yearsAnd yeah, it was a typo. I wanted to say Batch.
-
-
dbenham over 10 yearsYour FOR /F loop is not doing anything constructive - it simply assigns
*
to FOR variable%%i
. The following one liner gives the exact same result as the above:findstr /m /c:toFind *
-
Alvaro over 10 yearsWhat do you mean? It seems to solve my problem with white spaces. Now in
%%i
i have the complete name of the file rather than just a part like I had before. -
dbenham over 10 yearsI didn't say your code doesn't work, I'm just saying it is needlessly complex, and it is not solving the problem like you think it is. See my answer
-
Alvaro over 10 yearsThanks mate!! Your second option worked like a charm! Outputting the results ready to use for PHP. Just another fast question. Is it possible to return them such an array somehow? I'm getting them like one only string separate by spaces. (on the PHP call to the .bat file)
-
dbenham over 10 years@Alvaro - I don't understand what you are asking. It sounds like it should be a new question, perhaps on StackOverflow, with a lot more details.
-
Alvaro over 10 yearsok, thanks anyway! Also, would this be the faster way to search a text through items?
-
dbenham over 10 years@Alvaro - I've never tested, but I doubt there is a significant speed difference in this case.