Differences between revisions 4 and 5
Revision 4 as of 2023-01-30 03:00:07
Size: 1484
Comment:
Revision 5 as of 2023-01-30 18:30:12
Size: 1732
Comment:
Deletions are marked like this. Additions are marked like this.
Line 49: Line 49:
||'''Variable''' ||'''Value''' ||
||`%CD%` ||current working directory, ending in a slash (`/`) unless in a drive root||
||`%TIME%` ||system time in a localized format ||
||`%DATE%` ||system date in a localized format ||
||`%RANDOM%` ||a generated pseudorandom number between 0 and 32767 ||
||`%ERRORLEVEL%` ||error level returned by the most recent command or script ||
||`%CMDEXTVERSION%`||version of Command Processor Extensions used by `cmd.exe` ||
||`%CMDCMDLINE%` ||command used to start `cmd.exe` ||
||'''Variable'''||'''Value''' ||
||`%#` ||number of arguments ||
||`%$` ||all arguments as a string ||
||`%*` ||all arguments as a string, unmodified by the `shift` [[Batch/BuiltinCommands#Shift|builtin]]||
||`%@` ||all arguments as a string , but each token is [[Batch/Quoting|quoted]] ||



=== Positional Variables ===

The name of a command is stored in `%0`.

The first argument to the command is stored in `%1`. And so on until the 9th argument, `%9`.

For additional arguments, use the `shift` [[Batch/BuiltinCommands#Shift|builtin]] to move them into the 9 accessible variables.

To get all arguments as a string (like `%$`) starting from index ''n'', try `%n$`. To get all arguments until index ''n'' (non-inclusive), try `%-n$`.

Batch File Variables


Declaration

Variables are declared with the set [[Batch/BuiltinCommands#Set|builtin.

set foo=bar


Usage

Variables are accessed by their name. A percent sign (%) must surround the name.

set bar=%foo%

If a variable's value includes a character that the shell will interpret specially, quote the variable.

set bar="%foo%"

If a variable is not set, no substitution occurs.

set MyVar=
echo %MyVar% # '%MyVar%'


Special Variables

Variable

Value

%#

number of arguments

%$

all arguments as a string

%*

all arguments as a string, unmodified by the shift builtin

%@

all arguments as a string , but each token is quoted

Positional Variables

The name of a command is stored in %0.

The first argument to the command is stored in %1. And so on until the 9th argument, %9.

For additional arguments, use the shift builtin to move them into the 9 accessible variables.

To get all arguments as a string (like %$) starting from index n, try %n$. To get all arguments until index n (non-inclusive), try %-n$.


CategoryRicottone

Batch/Variables (last edited 2023-01-30 18:52:07 by DominicRicottone)