I’m trying to write a script that runs several operations using a starting variable but it doesn’t work: the variable is only correct the first time it is parsed. Do you know what I am doing wrong? Is this a limitation of batch files or have I done something wrong? The enabled expansion technique doesn’t work either.
SET FILE1=zr1
SET FILE2=za1
SET FILE3=za2
FOR /L %%I IN (1,1,3) DO (
-o "%%FILE%%I%%.out"
ECHO %%FILE%%I%%
CD p:\compress\compare
XCOPY "p:\compress\compare\%%FILE%%I%%.out"
RENAME "%%FILE%%I%%.out" "TEST"
XCOPY "p:\compress\compare\%%FILE%%I%%.out" /e
RENAME "%%FILE%%I%%.out" "GOOD"
COMPARE
RENAME "OUTPUT" "%%FILE%%I%%.out"
CD p:\compress\outputFILEs
XCOPY "p:\compress\compare\%%FILE%%I%%.out" /e
)
You need to properly use Delayed Expansion. Remember that variable expansions are achieved in two stages: %percents% first in left to right order, and then !exclamations!:
For a more detailed explanation, see this answer.
Also, I suggest you to use array notation for this type of management this way:
The reasons to do that are explained here