loops in DOS BATCH files

@echo off

rem a quick working example of loops in DOS BATCH files, by Daren Freckleton
rem
rem essential components:
rem 1. SET statement with name of list of variables on the left;
rem 2. SET statement with comma-separated list of variables on the right;
rem 3. FOR statement with name of list substituting for variable list;
rem 4. FOR statement with action to take on the right.

set var_list=1,2,3,4

for %%i in (%var_list%) do echo %%i

To use a commandline with multiple parameters:

for %%i in (%var_list%) do MYPROG.EXE param1 %%i param3

To execute more than one statement, use the CALL command to run a second batch file:

for %%i in (%var_list%) do call SCRIPT.BAT %%i

Note: the MS-DOS documentation appears hazy about this point: the %%i variable must be one character in length ONLY. Incorrect:

for %%filename in (%filelist%) do echo %%filename

Correct:

for %%f in (%filelist%) do echo %%f
 search:
this site only