Classic for-loop in a windows batch file

Question: How can a classic for loop

for (int i=0;i<10;++i) {
// do stuff
}

be realized in a windows batch file?

Answer:

echo off
SET /a i=0

:loop
IF %i%==10 GOTO END
echo This is iteration %i%.
SET /a i=%i%+1
GOTO LOOP

:end
echo That’s it!

The answer uses goto and the /A option of the set command. Without that option, SET interprets the right side of the equals sign as a string and i would contain ‘i+1’, then ‘i+1+1’ and so on. To make SET interpret the right side as an arithmetic operation, the option /A is required. Enter SET /? to get a detailed explanation of the SET command’s capabilities.

11 thoughts on “Classic for-loop in a windows batch file

  1. Aditya

    The arthametic is very starnge. Its working out side of loop but not working inside the loop 🙁

    Here is my code : (Retrive.bat)
    set i=1
    echo %i%
    set /a i = %i% + 1
    echo %i%
    for /r %%x in (%1) do (
    set /a i = %i% + 1
    echo %i%
    copy /-y “%%x” %2%i%
    :: all files with same name, so i need to append a counter
    )

    output :
    D:\zzz>Retrive *.html d:\zzz\*.html

    D:\zzz>set i=1

    D:\zzz>echo 1
    1

    D:\zzz>set /a i = 1 + 1

    D:\zzz>echo 2
    2

    D:\zzz>for /R %x in (*.html) do (
    set /a i = 2 + 1
    echo 2
    copy /-y “%x” d:\zzz\*.html2
    )

    D:\zzz>(
    set /a i = 2 + 1
    echo 2
    copy /-y “D:\zzz\CN=dashdash.html” d:\zzz\*.html2
    )
    2
    Overwrite d:\zzz\CN=dashdash.html2? (Yes/No/All):

    I am expecting i to be 3 …but ist still 2 🙁

    Reply
  2. admin Post author

    Hi Aditya!
    Now that’s an interesting question. Basically, the problem is, that SET does not work correct within for loops. At least not by default.
    Basically, what you need to do is to start your shell with the parameter /V which enables “delayed environment variable expansion”. You can find more about that by typing “cmd /?”.
    The other thing you need to do is to replace every occurence of “%i%” inside your loop with “!i!”. Type “set /?” to see a few examples and more explanation on delayed environment variable expansion.
    That topic would be worth a post if there weren’t such a good one already:
    http://www.robvanderwoude.com/variableexpansion.php
    HTH!
    Tom

    Reply
  3. David

    Here’s two more versions:

    —————————
    @echo off
    @FOR /L %%i in (0,1, 9) DO (
    echo This is iteration %%i.
    echo This is still iteration %%i
    )
    echo That’s it!
    —————————

    And my new favorite:

    —————————
    ::MAIN ROUTINE
    @FOR /L %%i in (0,1, 9) DO @CALL :PROCESS %%i
    echo That’s it!
    GOTO END

    :: SUBROUTINE PROCESS
    :PROCESS
    echo This is iteration %1.
    echo This is still iteration %1
    GOTO:EOF

    :END
    —————————

    Reply
  4. Dinesh

    In above example the for loop will run thru 10 times.
    But Instead of 9 in (0,1, 9), i want to add a variable like N. N will be supplied during run time. I tried as below (0,1, N). but its not working:( Please help me. Thanks in Advance

    Reply
  5. Ph4nT0m1m3

    heres mine, i just tweak your code to make it more simplier

    @echo off
    set /a count=0
    :countStart
    if %count%==10 goto countEnd
    echo hello world
    set /a count+=1
    goto countStart
    :countEnd
    pause

    Reply
  6. MichaelD

    I have a batch file that uses RoboCopy to copy files from network drives and local hard drives. It uses five variables. Four are text strings with the fifth being a six digit integer. The batch file works great for cases where data for one user is needed, but I have a need now to use this batch file to copy data from several users. I can place the variables in a CSV file and assign them %1, %2, etc in the batch file, but how do I get the batch file to loop through this list moving to the next in the list when the data copy has been completed for an individual user?

    Thanks for any help you can offer!

    Reply
  7. Proskit

    what cross-jumpin’ codes you publish, guys?
    please see this one:

    set /a i=0
    :Loop

    echo this is the body of the loop

    set /a i+=1
    if not %i%==10 goto Loop

    Reply

Leave a Reply to admin Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.