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?

  • The 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 like for /f, for which the syntax would be delims= instead of delims:. – Marcks Thomas Jan 15 '14 at 14:12
  • @MarcksThomas I just want to iterate over the elements of the folder. Don't know other way to do it. – Alvaro Jan 15 '14 at 14:20
  • I took it from: stackoverflow.com/questions/138497/… – Alvaro Jan 15 '14 at 14:26
  • And yeah, it was a typo. I wanted to say Batch. – Alvaro Jan 15 '14 at 14:40

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 *

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.

  • Thanks 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) – Alvaro Jan 15 '14 at 15:29
  • @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. – dbenham Jan 15 '14 at 15:37
  • ok, thanks anyway! Also, would this be the faster way to search a text through items? – Alvaro Jan 15 '14 at 15:38
  • @Alvaro - I've never tested, but I doubt there is a significant speed difference in this case. – dbenham Jan 15 '14 at 15:48

I found the solution.

It seems to work with double quotes on the in clause and with an /F instead of /R/ for thefor`:

@echo off
for /f  %%i in ("*") do  (
    findstr /m /C:toFind "%%i"
  • Your 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 * – dbenham Jan 15 '14 at 15:03
  • What 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. – Alvaro Jan 15 '14 at 15:08
  • 1
    I 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 – dbenham Jan 15 '14 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.