windows 如何从第 N 个位置获取批处理文件参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/382587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get batch file parameters from Nth position on?
提问by matt wilkie
Further to How to Pass Command Line Parameters in batch filehow does one get the rest of the parameters with specifying them exactly? I don't want to use SHIFT because I don't know how many parameters there might be and would like to avoid counting them, if I can.
进一步如何在批处理文件中传递命令行参数如何通过准确指定其余参数?我不想使用 SHIFT,因为我不知道可能有多少个参数,如果可以的话,我想避免计算它们。
For example, given this batch file:
例如,给定这个批处理文件:
@echo off
set par1=%1
set par2=%2
set par3=%3
set therest=%???
echo the script is %0
echo Parameter 1 is %par1%
echo Parameter 2 is %par2%
echo Parameter 3 is %par3%
echo and the rest are %therest%
Running mybatch opt1 opt2 opt3 opt4 opt5 ...opt20
would yield:
运行mybatch opt1 opt2 opt3 opt4 opt5 ...opt20
将产生:
the script is mybatch
Parameter 1 is opt1
Parameter 2 is opt2
Parameter 3 is opt3
and the rest are opt4 opt5 ...opt20
I know %*
gives all the parameters, but I don't wan't the first three (for example).
我知道%*
给出了所有参数,但我不想要前三个(例如)。
回答by Patrick Cuff
Here's how you can do it without using SHIFT
:
以下是不使用的方法SHIFT
:
@echo off
for /f "tokens=1-3*" %%a in ("%*") do (
set par1=%%a
set par2=%%b
set par3=%%c
set therest=%%d
)
echo the script is %0
echo Parameter 1 is %par1%
echo Parameter 2 is %par2%
echo Parameter 3 is %par3%
echo and the rest are %therest%
回答by aschipfl
The following code uses shift
, but it avoids to parse the command line using for
and lets the command line interpreter do this job (regard that for
does not parse double-quotes properly, for instance argument set A B" "C
is interpreted as 3 arguments A
, B"
, "C
by for
, but as 2 arguments A
, B" "C
by the interpreter; this behaviour prevents quoted path arguments like "C:\Program Files\"
from being handled correctly):
下面的代码使用shift
,但它避免使用解析命令行for
并让命令行解释器完成这项工作(关于for
没有正确解析双引号,例如参数集A B" "C
被解释为 3 个参数A
, B"
, "C
by for
, 但作为 2 arguments A
,B" "C
由解释器执行;此行为会阻止"C:\Program Files\"
正确处理引用的路径参数):
@echo off
set "par1=%1" & shift /1
set "par2=%1" & shift /1
set "par3=%1" & shift /1
set therest=
set delim=
:REPEAT
if "%1"=="" goto :UNTIL
set "therest=%therest%%delim%%1"
set "delim= "
shift /1
goto :REPEAT
:UNTIL
echo the script is "%0"
echo Parameter 1 is "%par1%"
echo Parameter 2 is "%par2%"
echo Parameter 3 is "%par3%"
echo and the rest are "%therest%"
rem.the additional double-quotes in the above echoes^
are intended to visualise potential whitespaces
The remaining arguments in %therest%
might not look like the way they were originally concerning the delimiters (remember the command line interpreter also treats TABs, ,
, ;
, =
as delimiters as well as all combinations), because all delimiters are replaced by a single space here. However, when passing %therest%
to some other command or batch file, it will be parsed correctly.
中的其余参数%therest%
可能看起来不像它们最初涉及分隔符的方式(请记住,命令行解释器也将制表符,
, ;
,=
视为分隔符以及所有组合),因为此处所有分隔符都被单个空格替换。但是,当传递%therest%
给其他命令或批处理文件时,它将被正确解析。
The only limitation I encountered so far applies to arguments containing the caret character ^
. Other limitations (related to <
, >
, |
, &
, "
) apply to the command line interpreter itself.
到目前为止,我遇到的唯一限制适用于包含插入字符的参数^
。其他限制(与<
、>
、|
、&
、 相关"
)适用于命令行解释器本身。
回答by Ntats
@ECHO OFF
SET REST=
::# Guess you want 3rd and on.
CALL :SUBPUSH 3 %*
::# ':~1' here is merely to drop leading space.
ECHO REST=%REST:~1%
GOTO :EOF
:SUBPUSH
SET /A LAST=%1-1
SHIFT
::# Let's throw the first two away.
FOR /L %%z in (1,1,%LAST%) do (
SHIFT
)
:aloop
SET PAR=%~1
IF "x%PAR%" == "x" (
GOTO :EOF
)
ECHO PAR=%PAR%
SET REST=%REST% "%PAR%"
SHIFT
GOTO aloop
GOTO :EOF
I like to use subroutines instead of EnableDelayedExpansion
. Above is extract from my dir/file pattern processing batch. Don't say this cannot handle arguments with =
, but at least can do quoted path with spaces, and wildcards.
我喜欢使用子程序而不是EnableDelayedExpansion
. 以上是我的目录/文件模式处理批处理的摘录。不要说这不能用 处理参数=
,但至少可以用空格和通配符来引用路径。
回答by Pavel P
@echo off
setlocal enabledelayedexpansion
set therest=;;;;;%*
set therest=!therest:;;;;;%1 %2 %3 =!
echo the script is %0
echo Parameter 1 is %1
echo Parameter 2 is %2
echo Parameter 3 is %3
echo and the rest are: %therest%
This works with quoted arguments and with arguments that have equal signs or commas, as long as first three arguments didn't have these special delimiter chars.
这适用于带引号的参数和具有等号或逗号的参数,只要前三个参数没有这些特殊的分隔符字符。
Sample output:
示例输出:
test_args.bat "1 1 1" 2 3 --a=b "x y z"
Parameter 1 is "1 1 1"
Parameter 2 is 2
Parameter 3 is 3
and the rest are: --a=b "x y z"
This works by replacing %1 %2 %3
in original command line %*
. The first five semicolons are there just to make sure that only the first occurrence of these %1 %2 %3
is replaced.
这通过%1 %2 %3
在原始命令行中替换来工作%*
。前五个分号只是为了确保只%1 %2 %3
替换第一次出现的分号。