Search notes:

cmd.exe: Splitting command line into arguments when calling a batch file

showArguments.bat

The following batch file displays the arguments with which it was called.
@set curArg=0
@setlocal enableDelayedExpansion
@for %%a in (%*) do @(
  set /a curArg=!curArg! +1
  echo arg !curArg! = %%a
)
Github repository about-cmd.exe, path: /parse-command-line/bat/showArguments.bat
Below, the results of some tests are displayed that try to show how the command line is parsed when a batch file is invoked.

simple test

The most simple test is probably something like that:
C:\> showArguments.bat foo bar baz
arg 1 = foo
arg 2 = bar
arg 3 = baz

Argument separators

White space and the characters ,, ; and = separate arguments from one another
C:\> showArguments.bat one,two;three=four five
C:\> arg 1 = one
C:\> arg 2 = two
C:\> arg 3 = three
C:\> arg 4 = four
C:\> arg 5 = five

Using apostrophes

Apostrophes can be used to pass multiple words as one argument. Note: the apostrophes are not removed.
C:\> showArguments "this is one argument" "This is the 2nd argument" "The 3rd"
arg 1 = "this is one argument"
arg 2 = "This is the 2nd argument"
arg 3 = "The 3rd"

Percent signs

C:\> showArguments.bat %%userprofile%% %^userprofile% %userprofile% 10% etc.
arg 1 = %C:\Users\Rene%
arg 2 = %userprofile%
arg 3 = C:\Users\Rene
arg 4 = 10%
arg 5 = etc.

Carets

C:\> showArguments.bat ^^^^ ^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
arg 1 =
arg 2 = ^
arg 3 = ^
arg 4 = ^^
arg 5 = ^^
arg 6 = ^^^

See also

Similar tests were conducted with executing an exe from the command line.
Parse the command line
Batch file variables %0, %1 etc. and %*
Programs and scripts that show command line arguments

Index