Share this page 

Arguments handling in a batch file Tag(s): Misc Prog HowTo


Each argument passed to BAT (or CMD) file is accessed with %1, %2, ...

Or use %1 every time and when the processing of the current argument is done then use the command shift to switch the next argument into %1. The following script accepts a list of files and type each one of them on the standard output:

[view.bat]

@echo off
if "%1" == "" goto error
rem - process each of the named files
:again
rem if %1 is blank, we are finished
if "%1" == "" goto end
echo.
echo Processing file %1...
type  %1
rem - shift the arguments and examine %1 again
shift
goto again
:error
echo missing argument!
echo usage  view file1.txt view2.txt ...
:end
echo.
echo Done.