Do Windows batch files have a %* construction?

11,229

Solution 1

Windows batch files (since Windows XP, but possibly earlier) support the %* construct, which evaluates to all the parameters from %1 onwards.

Unfortunately, this doesn't honour the SHIFT command, so the following won't work:

@echo off
set EATEN=%1
shift

call other.bat %*

It'll still pass the first parameter on to the second batch file.

Solution 2

You can gather all the args together using something like:

set args=%1
shift
:start
if [%1] == [] goto done
set args=%args% %1
shift
goto start

:done
(use %args% here)

This works regardless of how many arguments there are.

Solution 3

If you want to skip the first 3 parameters, for example, and pass on the rest:

@echo off
for /f "tokens=1-3*" %%a in ("%*") do (
   set par1=%%a
   set par2=%%b
   set par3=%%c
   set therest=%%d
   )

call other.bat %therest%

There may be problems with quotes in parameters.

kudos to Patrick Cuff, https://stackoverflow.com/questions/382587/how-to-get-batch-file-parameters-from-nth-position-on

Solution 4

Yes they do.

You can write your example as follows:

@echo off
call other.bat %*

Also: shift is supported on windows... See this.
But indeed, that last link confirms that shift doesn't work in combination with %*:

• Using %* with shift
  Shift has no affect on the %* batch parameter.

Share:
11,229

Related videos on Youtube

Paula
Author by

Paula

I was a Windows programmer (15 years of C++, C#, SQL Server), but I took a leap of faith and now I'm a backend software developer, using Erlang and Elixir, at Twilio.

Updated on September 17, 2022

Comments

  • Paula
    Paula over 1 year

    In a batch file, do I have to do (e.g.) the following?

    @echo off
    call other.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
    
  • fretje
    fretje almost 15 years
    Sorry links were bad... should be fixed now.
  • Wedge
    Wedge almost 15 years
    (quote)Using %* with shift Shift has no affect on the %* batch parameter.(/quote)
  • fretje
    fretje almost 15 years
    Why the downvote? The question was about %* not about shift.
  • Marcelo Neves
    Marcelo Neves almost 15 years
    Your first answer only had "Yes they do" which wasn't an obvious link (or answer). Your last edit is much more of a decent answer, perhaps that's why...
  • fretje
    fretje almost 15 years
    Actually the downvote happened after i added more explanation. And I agree that the link on "Yes they do" wasn't obvious, but it was an exact answer to the question at hand...
  • fretje
    fretje almost 15 years
    @Roger: Off topic, but how come you already have 121 reputation, and only 2 upvotes? That means you started at 101 in stead of at 1?
  • Dharma Leonardi
    Dharma Leonardi almost 13 years
    Preserves all special delimiters. Supports as many Parameters as can be fitted in an 8175 character per command line limit (including batch filename, CRLF and batch path if specified). Parameters can be grouped with double quotes (eg. "sw1=/quiet /skip:1 /start=3"). Note: Double quotes are preserved. Behaviour of double quotes are expected (even if used incorrectly, eg. used only singly or " " or ""). Please debug the code first before making a bug report.
  • Teja Vemulapalli
    Teja Vemulapalli over 5 years
    That's very elaborate answer showing lots of debug info, though it does not directly answer the original question: how to call other.bat with given parameters:( Where do I write "other.bat"? Is it possible to remove "Parameter index(es) to extract" query? Also, cls is not needed in the final solution.
  • Aaron Digulla
    Aaron Digulla about 5 years
    Note: %* preserves whitespace/quotes.