0

As you can see I am new in the batch files, I have written a script in windows 2003 environment for print an increased value of the variable counter and also I have applied the if condition which is not working in the for loop, though outside the for it is showing the correct number of variable value.

@echo off
SetLocal EnableDelayedExpansion
cls
set /A counter = 1
for C:\Scripts\LogFiles\  %%a in (*.txt) do ( echo %counter%
set /A counter += 1
echo %%a
if %counter% ==2 (echo test)
)
Decado
  • 1,949
  • 11
  • 17

1 Answers1

1

Delayed expansion requires you to use ! instead of % to expand variables. So it should be !counter!.

All environment variables of the form %counter% are expanded during parsing of a command (for for with a block this includes the complete block), so you'll only see the value a variable had before the loop since by the time the loop runs there are no variables anymore; only values.

Joey
  • 1,853
  • 11
  • 13