10

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
1
  • Similar to $@ on Unix – An̲̳̳drew Oct 21 '10 at 21:05
11

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.

1
  • 2
    Note: %* preserves whitespace/quotes. – Aaron Digulla Feb 4 '19 at 13:14
11

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.

2

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

1

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.

6
  • Sorry links were bad... should be fixed now. – fretje Jun 9 '09 at 10:06
  • (quote)Using %* with shift Shift has no affect on the %* batch parameter.(/quote) – Wedge Jun 9 '09 at 10:08
  • Why the downvote? The question was about %* not about shift. – fretje Jun 9 '09 at 10:13
  • 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... – thijs Jun 9 '09 at 11:01
  • 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 Jun 9 '09 at 11:28
0
@echo off
cls
setlocal enabledelayedexpansion

:: Disable the display of information by changing [set "_ENABLE_INFO="] to [set "_ENABLE_INFO=::"].
set "_ENABLE_INFO="
set _DBQ="

set "_PARMS.ORIGINAL=%*" && if defined _PARMS.ORIGINAL (
   if not defined _PARMS.IDX (
      echo Parameter Index^(es^) to extract eg.:
      echo 1 2 5 9 11 39 400
      set /p "_PARMS.IDX=>"
   )
   set "_PARMS.IDX=¡!_PARMS.IDX: =¡ ¡!¡"
   set "_PARMS.IDX=!_PARMS.IDX:¡¡=!"
   set "_PARMS.IDX=!_PARMS.IDX: =!"
   call :_FN_PROC.STRING %*
%_ENABLE_INFO% goto :FINISH
   goto :eof
) else (
   echo No input parameters.
)
goto :eof


:_FN_PROC.STRING
set "_PARMS.PROCESS.STRING=%*"
:_FN_PROC.STRING.LOOP
set "_PARMS.PROCESS.STRING=!_PARMS.PROCESS.STRING:%_DBQ%=¡!"
set /a _PARMS.COUNT+=1
for /f "tokens=1* delims= " %%a in ("!_PARMS.PROCESS.STRING!") do (
   set _PARMS.TMP.CHECK=%%a
   set _PARMS.PROCESS.STRING=%%b
   if "!_PARMS.TMP.CHECK!" NEQ "!_PARMS.TMP.CHECK:¡=!" (
      for /f "tokens=1* delims=¡" %%c in ("%%a !_PARMS.PROCESS.STRING!") do (
         set _PARMS.TMP.CHECK="%%c"
         set _PARMS.PROCESS.STRING=%%d
      )
   )
)

if "!_PARMS.IDX:¡%_PARMS.COUNT%¡=!" EQU "!_PARMS.IDX!" (
   set _PARMS.REMAINING=!_PARMS.REMAINING! !_PARMS.TMP.CHECK!
   set _PARMS.IDX=!_PARMS.IDX!{%_PARMS.COUNT%}
) else (
   set _PARMS.EXTRACTED=!_PARMS.EXTRACTED! !_PARMS.TMP.CHECK!
   set _PARMS.IDX=!_PARMS.IDX:¡%_PARMS.COUNT%¡=!
   set _PARMS.IDX=!_PARMS.IDX![%_PARMS.COUNT%]
)

if "!_PARMS.PROCESS.STRING: =!"=="" (set _PARMS.PROCESS.STRING=)
if defined _PARMS.PROCESS.STRING (goto :_FN_PROC.STRING.LOOP)

set _DBQ=
set _PARMS.TMP.CHECK=
if defined _PARMS.REMAINING (set _PARMS.REMAINING=!_PARMS.REMAINING:~1!)
if defined _PARMS.EXTRACTED (set _PARMS.EXTRACTED=!_PARMS.EXTRACTED:~1!)
goto :eof


:FINISH
echo.
echo _PARMS.IDX ^:
echo    ¡^=¡ no Parameter Index was specified for extraction.
echo    ¡n¡ nth Parameter was not processed (Index larger than Parameters Count).
echo    [n] nth Parameter was Extracted.
echo    {n} nth Parameter was Ignored (Remaining).
echo.
set _
goto :eof
2
  • 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. – Dharma Leonardi May 18 '11 at 7:50
  • 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. – izogfif Sep 17 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.