0

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 
) 
Chris
  • 44,602
  • 16
  • 137
  • 156

1 Answers1

1

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

ECHO !FILE%%I! 

For a more detailed explanation, see this answer.

Also, I suggest you to use array notation for this type of management this way:

SET FILE[1]=zr1
SET FILE[2]=za1
SET FILE[3]=za2
. . .
ECHO !FILE[%%I]!

The reasons to do that are explained here

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108