Jump to Real's How-to Main page

Arguments handling in a batch file

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.

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2007
[ home ]