Skip to content

Batch Script For Loop with Command Line Arguments

Learn how to process command line arguments in a Windows batch script using a for loop to iterate through each parameter passed to the script.

For in Command Line Arguments

The for instruction can be used to control command line arguments.

example.batbatch
@echo off 
:Loop 

if "%1"=="" goto completed 
for %%f in (%1) do echo %%f 
shift 
goto Loop 
:completed

Suppose the above code is stored in a file called example.bat. The above command will produce the following result if the batch file passes command line arguments 1, 2 and 3 as example.bat 1 2 3.

Outputplaintext
1
2
3
Navigation