I have a simple FOR loop inside of a batch file I'm creating that isn't quite working the way I'm expecting. The FOR loop iterates through lines of a text file, and on each iteration, I use an IF statement to make a quick comparison on the current line, and do some work if it evaluates to true. Here is the code:
SETLOCAL ENABLEDELAYEDEXPANSION
set /a sitecounts=1
set /a input=34
FOR /F "tokens=1,2 delims=^/" %%G IN (file.txt) DO (
IF %sitecounts% == %input% (set /a selectedsitepath=%logfilepath%W3SVC%%H)
set /a sitecounts=!sitecounts!+1
echo !sitecounts!
)
I'm running into a weird issue where, the sitecounts variable (my counter) actually increments properly with each loop (I prove this by echoing out the count with the last statement) -- BUT upon the next iteration %sitecounts% still shows that it has the value of '1', which was the initial value.
If I change the code to:
IF !sitecounts! == ...
...then the IF statement appears to treat that value as pure TEXT! I've set ENABLEDELAYEDEXPANSION to handle variables within loops, but not sure what to do next. Any help is appreciated!
This time I actually tested it....
Change the line
IF %sitecounts% == %input% (set /a selectedsitepath=%logfilepath%W3SVC%%H)
into
IF !sitecounts! == %input% (set selectedsitepath=%logfilepath%W3SVC%%H)
%sitecounts% needs to be !sitecounts! in order to be evaluatedset /a selectedsitepath=%logfilepath%W3SVC%%H doesn't work because it is not a numerical expression:
The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated.
Just omit the switch (/p is not going to work neither, that's for a prompt)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With