pass variables between nested for loops

Microsoft Windows xp professional editio...
October 16, 2009 at 15:20:47
Specs: XP
I am attemping to run a script which will perform a for loop for each computer in a text file. I am using the following nested for loop:

for /F "tokens=1 usebackq delims=," %%n in (`more %1`) do (
for /R \\%%n\e$\path\to\directory %%i in (*.nk) do (
sed -i.bak s/F:/E:/ %%i
)
)

If I take the value for %%n and plug it directly into the 2nd for loop, it works fine, so it would seem that the value of %%n is not being passed to the 2nd for loop.
Any thoughts or suggestions would be greatly appreciated. I have tried using setlocal, and call but have not had any luck so far.


See More: pass variables between nested for loops

Report •


#1
October 16, 2009 at 15:36:31
This defiantly belongs in the programming forum.

To answer your quest change your /F to a /D because you are using it in an UNC Path but /F will return a file name not a directory name.


Report •

#2
October 16, 2009 at 21:52:11
ace,

Good point.

I wonder if you're using enableDELAYedexpansion with setlocal.

You might try preceeding sed with ECHO to see what %%i is doing.

I never use backq nor have I seen MORE inside () but I'll take your word for it that it's legit.


=====================================
Helping others achieve escape felicity

M2


Report •

#3
October 19, 2009 at 09:05:12
Thanks for the replies. I've fiddled quite a bit with those suggestions but I haven't had any luck yet. My question is related specifically to how variables work in for loops.
If I do an echo %%n and %%i, %%n does not get expanded.
I am wondering if these means it instance of "for" is started in its own shell instance thereby losing any previously set variables in the script as it is running in its own shell.
Here's a modified version of the script which fails:
echo off
for /F "usebackq delims=" %%n in (`more %1`) do (
echo f1n=%%n
for /R \\%%n\e$\zzzReel\Headcomps\test\test2 %%i in (*.nk) do (
echo f2n=%%n
echo f2i=%%i
)
)
This script returns the following:
f1n=3dstf12

And then when I manually add in my variable:
echo off
for /F "usebackq delims=" %%n in (`more %1`) do (
echo f1n=%%n
for /R \\3dstf12\e$\zzzReel\Headcomps\test\test2 %%i in (*.nk) do (
echo f2n=%%n
echo f2i=%%i
)
)

This returns:
f1n=3dstf12
f2n=3dstf12
f2i=\\3dstf12\e$\zzzReel\Headcomps\test\test2

This is really interesting to me as it means that my problem variable - %%n, is actually being passed just fine to the 2nd for loop - it just isn't getting expanded as an argument in the actual 2nd for loop command.

Is it possible for me to move this thread to the programmer section? I don't want to post twice.
cheers


Report •

Related Solutions

#4
October 19, 2009 at 14:20:42
Have you tried switching the /F with /D yet like I said?

Report •

#5
October 19, 2009 at 19:20:24
Yes and that didn't work.

Report •


Ask Question