variable assignment inside for loop ?

August 28, 2009 at 02:40:35
Specs: Windows 2000 NT, 2GHz / 2GB
Hi Guys,

Just another newbie and possibly simple problem.

I got asked to write a batch program to copy one file (Filea) from one location to multiple folders.
I got the list of directories the fileA is supposed to go to and created following inside file called 'mycopy.bat'

rem @echo off

dir /B "C:\symmetrix" >"%temp%\symmList.txt"

set "symmlist=%temp%\symmList.txt"
set "origPath=H:\tests\GetNewFilesOnly.bat"

For /F "tokens=* delims=" %%i in (%symmList%) do (set pathL=C:\Symmetrix\%%i\daily
copy /V %origPath% %pathL%
)

I got the idea that the problem lies in the assignment of the %pathL% variable.
When the program executes it either produces this:

set pathL=C:\Symmetrix\000187800023\daily
copy /V H:\tests\GetNewFilesOnly.bat
)
The file cannot be copied onto itself.
0 file(s) copied.

or

set pathL=C:\Symm\000187800023\daily
copy /V H:\tests\GetNewFilesOnly.bat C:\Symm\000287700326\daily
)
1 file(s) copied.

Where 000287700326 is the last line in the symmList.txt
Hence, files never go to other folders even though pathL variable changes with every iteration and should point to the right location.

I've tried clearing pathL after each iteration by adding set pathL= after copy, but it seemes that interpreter jumps first to that line and then to copy command.

For various reasons I can't use third party apps and am stuck with batch files (no Perl, no VBS).

Any suggestions about what am I missing??


See More: variable assignment inside for loop ?

Report •


#1
August 28, 2009 at 03:34:40
Code blocks(inside parentheses) are treated like one line for variable expansion. Standard variables are substituted before the line is executed.

With 2000 and above delayed expansion is available, though it requires variables to be local. To use it add the line "setlocal enabledelayedexpansion" to the top of your script or before your loops. Variables will continue to be marked with %'s unless you want the variable to be delayed, in which case you use !'s instead.

Just be aware that delayed expansion has a caveat, !'s that are part of file names ect. dissapear.

All that being said your script doesn't even need it:

@echo off
set "origPath=H:\tests\GetNewFilesOnly.bat"
For /F "delims=" %%i in ('dir /B "C:\symmetrix" ') do (
    copy /-y /V "%origPath%" "C:\Symmetrix\%%i\daily"
)


Report •

#2
August 28, 2009 at 03:42:37
I just re-read you post and realised I glossed over this:

I got asked to write a batch program to copy one file (Filea) from one location to multiple folders.

For dir to only list directories you also need to add the "/ad" switch.


Report •

#3
August 28, 2009 at 03:50:13
Cheers, I'll give it a try. But I'll definitely come back before starting to pull my hair out :)

That point was really helpful though:
Standard variables are substituted before the line is executed.

That explains why the last line of text file is substituted in pathL- it's already been assigned to the environmental variable when the script ran previously.

Guess I've missed that when 'trying' to learn a bit of batch scripting


Edit:
Works like a charm :) one hurdle out of the way-more to come :(


Report •

Related Solutions


Ask Question