Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

1 Answer 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.

share|improve this answer

Your Answer

 
discard

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.