Computing.Net > Forums > Programming > variable assignment inside for loop ?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

variable assignment inside for loop ?

Reply to Message Icon

Name: sally.hayes
Date: August 28, 2009 at 02:40:35 Pacific
OS: Windows 2000 NT
CPU/Ram: 2GHz / 2GB
Subcategory: Batch
Tags: batch, for loop, environment variables, overwriting variables
Comment:

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??



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: August 28, 2009 at 03:34:40 Pacific
Reply:

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"
)


0

Response Number 2
Name: Judago
Date: August 28, 2009 at 03:42:37 Pacific
Reply:

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.


0

Response Number 3
Name: sally.hayes
Date: August 28, 2009 at 03:50:13 Pacific
Reply:

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 :(


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: variable assignment inside for loop ?

for loop find result into variable www.computing.net/answers/programming/for-loop-find-result-into-variable/20281.html

FOR loops in C www.computing.net/answers/programming/for-loops-in-c/8449.html

Display numbers with C++ for loops www.computing.net/answers/programming/display-numbers-with-c-for-loops/7449.html