Search notes:

cmd.exe - for

for is affected by whether command extensions are enabled or disabled.

Iterating over items

The for %%a in (itemOne itemTwo itemThree) do … construct allows to iterate over the items within the parentheses. For each item, the text of the item is assigned to the single letter (!) variable after the for and then the statement after the do executed.
If multiple statements are to be executed, they need to be enclosed within parentheses.
The following example has two loops where the two variables %%a and %%b are iterated from 0 to f. Then, these variables are used to set the console's color. After the console's colors were changed, timeout.exe is invoked to sleep for a second.
Finally, when all 256 color combinations were iterated over, the whole process starts again (using the goto statement.
@echo off
setlocal

set digits=0 1 2 3 4 5 6 7 8 9 a b c d e f

:doAgain

for %%a in (%digits%) do (
  for %%b in (%digits%) do (
    color %%a%%b
    timeout /t 1 > nul
  )
)

goto doAgain
Github repository about-cmd.exe, path: /commands/for/iterateColors.bat

Number of percent signs in a batch file and in the command line

If the for command is used in a batch file, the looping-variable needs to percent signs (for example %%X) On the other hand, if the for command is directly entered into the cmd.exe prompt, only one percent sign is needed (for example %X).

See also

for /f
for /l
cmd.exe: Built-in commands

Index