Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting in a FOR loop using Windows Batch script

Can anyone explain this? I am able to count in a loop using the Windows command prompt, using this method:

SET /A XCOUNT=0 :loop SET /A XCOUNT+=1 echo %XCOUNT% IF "%XCOUNT%" == "4" (   GOTO end ) ELSE (   GOTO loop ) :end 

But this method does not work (it prints out "1" for each line in the file). It acts like the variable is out of scope:

SET /A COUNT=1 FOR /F "tokens=*" %%A IN (config.properties) DO (   SET /A COUNT+=1   ECHO %COUNT% ) 
like image 558
djangofan Avatar asked Sep 22 '11 23:09

djangofan


People also ask

How does for loop work in batch?

For loop (default) of Batch language is used to iterate over a list of files. Example: copy some files into a directory (Note: the files to be copied into the target directory need to be in the same disk drive).

What does %% mean in batch script?

%%i is simply the loop variable. This is explained in the documentation for the for command, which you can get by typing for /? at the command prompt.


2 Answers

It's not working because the entire for loop (from the for to the final closing parenthesis, including the commands between those) is being evaluated when it's encountered, before it begins executing.

In other words, %count% is replaced with its value 1 before running the loop.

What you need is something like:

setlocal enableextensions enabledelayedexpansion set /a count = 1 for /f "tokens=*" %%a in (config.properties) do (   set /a count += 1   echo !count! ) endlocal 

Delayed expansion using ! instead of % will give you the expected behaviour. See also here.


Also keep in mind that setlocal/endlocal actually limit scope of things changed inside so that they don't leak out. If you want to use count after the endlocal, you have to use a "trick" made possible by the very problem you're having:

endlocal && set count=%count% 

Let's say count has become 7 within the inner scope. Because the entire command is interpreted before execution, it effectively becomes:

endlocal && set count=7 

Then, when it's executed, the inner scope is closed off, returning count to it's original value. But, since the setting of count to seven happens in the outer scope, it's effectively leaking the information you need.

You can string together multiple sub-commands to leak as much information as you need:

endlocal && set count=%count% && set something_else=%something_else% 
like image 148
paxdiablo Avatar answered Sep 22 '22 18:09

paxdiablo


for a = 1 to 100 step 1

Command line in Windows . Please use %%a if running in Batch file.

    for /L %a in (1,1,100) Do echo %a  
like image 34
sujith Avatar answered Sep 19 '22 18:09

sujith