72

Does %* in batch file mean all command line arguments?

Wasif
  • 7,984
  • 2
  • 19
  • 32
Matt
  • 6,231
  • 20
  • 57
  • 79

2 Answers2

96

Yes. According to the official Microsoft documentation:

The %* batch parameter is a wildcard reference to all the arguments, not including %0, that are passed to the batch file.

Matt Solnit
  • 1,590
  • 12
  • 12
  • 8
    note: if you have 30 words separated with spaces as argument, you can only take the 9 first words with %i, with i from 1 to 9, but with %* you can take all the 30 words – kokbira Apr 25 '11 at 20:38
  • 6
    @kokbira or you can use [shift](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shift.mspx?mfr=true) to access the rest – TWiStErRob Nov 10 '14 at 13:38
  • 2
    @TWiStErRob - I'm sure you know it but to be clear... even with %* you need to use [shift](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shift.mspx?mfr=true) to access parameters beyond %9. The advantage of using %* is you can pass the entire ORIGINAL parameter list (including parameters that have been "shifted out") to an external batch or other program, or with a `call :label %*`, even if you don't know how many parameters are in the list (or if it's more than 9). – Kevin Fegan Feb 16 '20 at 22:44
  • 4
    Is this quoted properly, like `"$@"` in sh? – mirabilos Jan 04 '22 at 22:31
1

Besides this, a comment by @kobkira notes that you can take only up to 9 arguments in conventional syntax. Like this if you want to get n number of arguments in separate array style variables, use this syntax:

@echo off & setlocal enabledelayedexpansion & set "n=30"
for /l %%a in (1,1,%n%) do (
  for /f "tokens=%%a delims= " %%b in ('echo %*') do (
    set "arg[%%~a]=%%~b"
  )
)
Wasif
  • 7,984
  • 2
  • 19
  • 32
  • Does *array style* mean you include indices into variable names? Batch file doesn't support private variables, every assignment exposes variable as environment var... (( – gavenkoa Nov 25 '22 at 22:20