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:


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

blog comments powered by Disqus