tags:

views:

33

answers:

1

1 Thanks for check this out The following batch file (findit.bat) works to find a string variable in a single directory

for %%a in (C:\myDir\*.txt) do find "%1" >> %%a >> output.txt

Example from command prompt:

c:> findit someword

all files that contain "someword" are written to > output.txt

The question is: how do I have this include subdirectories? I have tried adding the /s switch (C:\myDir /s) But that that doesn't work. It stays in the root of \Mydir :( Any ideas? For ever; Goto 1, Until done :)

A: 

According to the Windows help, the FOR command supports the /R switch to recurse into sub-directories:

for /R %%a in (C:\myDir\*.txt) do find "%1" %%a >> output.txt

Edit: corrected the usage of the find command.

The only drawback is that this will also search in output.txt if that is created in c:\myDir

a_horse_with_no_name
Thanks, but the output is still for the root directory. I did a c:>for /? > for.txt and notepadded it to get help from the DOS's mouth and it agrees that the /R switch should work. back to 1:
Craig Cutler
I think your usage of find is wrong. You are only using %1 in the find command. You are not passing a filename to it, so I guess it should be: find "%1" %%A >> output.txt?Using "for /R %%a in (*.txt) do echo "%%a"" worked for me (displayed all text files in all sub-directories)
a_horse_with_no_name
Yes, that returns the NAMES of the text files in all subdirs, but not the content of the text files specified in %1Removing the /r switch. I added the /s switch after c:\myDir /s *.txt and that finds only one occurrence of the search string. I will struggle with this some more tomorrow after the joy of hand-mixing concrete. I'd rather fight with DOS.. Will update then
Craig Cutler
Again: your usage of the find command is wrong. I changed my example code to correct that
a_horse_with_no_name
Your changed example works great for echoing directories and file names to the screen. My first example works great for outputting a text file with any finds of a search string variable %1. There is nothing "wrong" with it. I just want the same function to perform on subdirectories as well and output it to a file. The /R switch does not work. That is what I am asking about.
Craig Cutler
Well my example outputs the search expression passed as %1 in all *.txt files in all subdirectories. Note the difference between find %1 >> %%a (which would searches the expression %1 from stdin and outputs the result to %%a) and my usage %1 %%a which searches expression %1 in the file denoted by %%aThe /R switch *does* work and will make the FOR loop recurse into all subdirectories.
a_horse_with_no_name
Thanks again, I will work with this and let you know what happens..
Craig Cutler
Got that working ..for one level of subdirs. Subdirs within subdirs are not being snagged. here's the batch file-for /r %%a in (*.txt) do find/i "%1" %%a >> output.txtany way you know how to get to the deeper level subdirs?
Craig Cutler
Spaces in directory names are causing those errs. The only residual is that the screen echos "File not found" followed by each directory and filename being processed. Any idea why?
Craig Cutler
Put quotes around the (second) %%a ("%%a") so that the filename passed to the find command is quoted
a_horse_with_no_name
Done! a_horse_with_no_name is AWESOME !
Craig Cutler