在批处理文件中,我是否必须执行以下操作?
@echo off
call other.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
答案1
Windows 批处理文件(自 Windows XP 起,但可能更早)支持该%*
构造,它可以计算从 %1 开始的所有参数。
不幸的是,这不遵守SHIFT
命令,因此以下命令不起作用:
@echo off
set EATEN=%1
shift
call other.bat %*
它仍会将第一个参数传递给第二个批处理文件。
答案2
您可以使用类似以下方法将所有参数收集在一起:
set args=%1
shift
:start
if [%1] == [] goto done
set args=%args% %1
shift
goto start
:done
(use %args% here)
无论有多少个参数,这都是有效的。
答案3
例如,如果您想跳过前 3 个参数并传递其余参数:
@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%
参数中的引号可能有问题。
向 Patrick Cuff 致敬,https://stackoverflow.com/questions/382587/how-to-get-batch-file-parameters-from-nth-position-on
答案4
@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