Loading...
 

Built-In Variables

Table of contents:

Command-line parameters

When you start a script, The entire command-line including parameters is assigned to the variables %0, %1, %2, %3 .... You can pass a lot of parameters (I stopped testing at 78), but the script can only access up to ten at a time.

%0 Variable

%0 initially stores the name of the script file. It holds whatever was specified on the command line including path if specified. The contents of %0 can be modified using the same tilde modifiers defined for the for command. For example, specifying %~n0 will trip out everything but the filename. And using %~p0 will print just the path of the batch file. Note that the tilde will retrieve file attributes such as the extension even if they were not passed on the command-line.

Shift

In order to access the remaining the command-line parameters, you can use the shift command to move the parameters forward in the variable list. Once issued, the %0 parameter will drop off the list. %1 will move to %0 and %2 into %1, etc.

REM Testcmd.cmd
@echo Here are the command line parameters
@echo %0, %1, %2
@echo Here is the command-line: %0  and here is just the filename with extension: %~n0%~x0  
shift
@echo %0, %1, %2


C:\WABS> c:\WABS\Testcmd.cmd Test 02
Here are the command line parameters
C:\WABS\Testcmd, Test, 02
Here is the command-line: C:\WABS\Testcmd and here is just the filename with extension: Testcmd.bat
Test, 02


C:\WABS> Testcmd.cmd Test 02
Here are the command line parameters
Testcmd, Test, 02
Test, 02




Created by system. Last Modification: Thursday 17 of November, 2016 22:35:41 EST by gmartin.