Need Help with for loop doing find in all subdirectories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Playthious
Posts: 2
Joined: 11 Nov 2012 22:55

Need Help with for loop doing find in all subdirectories

#1 Post by Playthious » 11 Nov 2012 23:01

Here is the code which I am trying to have input a string and search the directory + all sub directories for the string and to print all of the occurrences to result.txt. However, "%var1" seems to be causing problems, as well as the fact that when I run the for command itself in the console, when it finds the first occurrence of the string it gets stuck and I have to do Ctrl + C and nothing is written to the result file.

Code: Select all

@echo off
set /p var1= Enter the String to Find:
for /F "delims=" %a in ('dir /B /S *.txt')
do @(find /i "%var1" "%a" 1>nul 2>&1 && find /i "%var1" "%a") >> result.txt


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need Help with for loop doing find in all subdirectories

#2 Post by foxidrive » 12 Nov 2012 06:41

Try this: it's a batch file and not meant for a cmdline prompt.

Edited to correct syntax:

Code: Select all

@echo off
set /p "var1=Enter the String to Find: "
for /F "delims=" %%a in ('dir /B /S *.txt 2^>nul ^| find /i "%var1%" ') do >>"result.txt" echo %%a
pause

Playthious
Posts: 2
Joined: 11 Nov 2012 22:55

Re: Need Help with for loop doing find in all subdirectories

#3 Post by Playthious » 12 Nov 2012 10:32

That seems to be getting stuck and only creates the results.txt with

"Enter the string to find:"

And when I close it I get the message
"The process tried to write to a nonexistent pipe."

Any ideas?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need Help with for loop doing find in all subdirectories

#4 Post by foxidrive » 12 Nov 2012 16:55

You didn't copy and paste it correctly. It finds the .txt files here.

Post Reply