Nested for loops using both /r and /f

July 6, 2010 at 11:24:51
Specs: Windows XP
I need to be able to locate all files with a certain extension(.xof) on a server which contains hudreds of different directories. I want to collect all the .ini files from only certain directories to save time. I thought that the batch file I wrote would do this but alas it doesnt work.

set block=%1
set adc=x:\releases\

for /f %%w in ('dir %adc%*%block%* /b') do (
echo adc=%adc%
echo %adc%%%w
echo "for /r %adc%%%w %%x in (*.xof) do echo found these xofs for %1 %%x"
"for /r %adc%%%w %%x in (*.xof) do echo found these xofs for %1 %%x"

On the echo return statement I see:
"for /r x:\releases\nb1007q4 %x in (*.xof) do echo found these xofs for 1007 %x"

But when it does the actual run I see:
The system cannot find the path specified.

And if I run the echo returned statement with out the "" 's on a command line the for loop does return the .xof files I'm looking for. I feel like there is either something very simple I'm missing or this just cant be done. Please help.


See More: Nested for loops using both /r and /f

Report •


#1
July 6, 2010 at 11:43:19
Sorry I realized as soon as I submitted this that I cant have "" 's with my actual for /r line. But once I remove those quotes then when my for /r step runs it searchs all directories on the server and not just the ones I'm trying to specify with the run /f step. Also the variable passing on the command line for %1 is 1007.

updated batch file:
for /f %%w in ('dir %adc%*%block%* /b') do (
echo adc=%adc%
echo %adc%%%w
echo "for /r %adc%%%w %%x in (*.xof) do echo found these xofs for %1 %%x"
for /r %adc%%%w %%x in (*.xof) do echo found these xofs for %1 %%x

Results I'm seeing now:
found these XOF's x:\releases\DT1001F4\A001\V64\BRZ\dt1001f4v64brza001.xof

What I expect to see is:
found these xofs for 1007 x:\releases\nb1007q4\a001\v32\eng\nb1007q4v32enga001.xof

I really hope that I'm explaing my problem well enough.


Report •

#2
July 6, 2010 at 11:47:14
Yeah, you can't nest them directly like that. I normally get around this by using CALL:
FOR /D %%w IN (x:\releases\*%1*) DO CALL :loop %%~Fw
GOTO :EoF
:loop
FOR /r "%*" %%z IN (*.xof) DO @ECHO %%~Fz


Report •

#3
July 6, 2010 at 13:08:09
Thanks a million. I didnt even think about doing a call. Works perfectly

Report •
Related Solutions


Ask Question