Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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)
share|improve this question

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.