windows 批处理参数:%1 之后的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/935609/
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
batch parameters: everything after %1
提问by noamtm
Duplicate:
复制:
- Is there a way to indicate the last n parameters in a batch file?
- how to get batch file parameters from Nth position on?
Clarification:I knew of the looping approach - this worked even before Command Extensions; I was hoping for something fun and undocumented like %~*1 or whatever - just like those documented at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true.
澄清:我知道循环方法 - 这甚至在命令扩展之前就有效;我希望有一些有趣且没有记录的东西,比如 %~*1 或其他任何东西 - 就像http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx 中记录的那些?mfr=真。
In a Windows batch file (with the so called "Command Extensions" on), %1 is the first argument, %2 is the second, etc. %* is all arguments concatenated.
在 Windows 批处理文件中(启用了所谓的“命令扩展”),%1 是第一个参数,%2 是第二个参数,等等。%* 是连接的所有参数。
My question: is there a way to get everything AFTER %2, for example?
我的问题:例如,有没有办法在 %2 之后获得所有内容?
I couldn't find such a thing, and it would be helpful for something I'm working on.
我找不到这样的东西,这对我正在做的事情会有帮助。
采纳答案by Samuel
I am not sure if there is a direct command but you can always use a simple loop and shift to get the result in a variable. Something like:
我不确定是否有直接命令,但您始终可以使用简单的循环和移位来获取变量中的结果。就像是:
@echo off set RESTVAR= shift :loop1 if "%1"=="" goto after_loop set RESTVAR=%RESTVAR% %1 shift goto loop1 :after_loop echo %RESTVAR%
Let me know if it helps!
如果有帮助,请告诉我!
回答by Max Truxa
There is a shorter solution (one-liner) utilizing the tokenization capabilities of for
loops:
有一个更短的解决方案(单行)利用for
循环的标记化功能:
:: all_but_first.bat
echo all: %*
for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b
echo all but first: %ALL_BUT_FIRST%
output:
输出:
> all_but_first.bat foo bar baz
all: foo bar baz
all but first: bar baz
回答by Raman Zhylich
The following will work for args with ", =, ' '. Based on Dmitry Sokolov answer. Fixed issue when second arg is the same as first arg.
以下将适用于带有 ", =, ' ' 的 args。基于 Dmitry Sokolov 的回答。修复了第二个 arg 与第一个 arg 相同的问题。
@echo off
echo %*
set _tail=%*
call set _tail=%%_tail:*%1=%%
echo %_tail%
回答by Dmitry Sokolov
The following will work for args with "
, =
, ' '
(as compared to @MaxTruxa answer)
以下内容适用于"
, =
, ' '
(与@MaxTruxa 答案相比)
echo %*
set _all=%*
call set _tail=%%_all:*%2=%%
set _tail=%2%_tail%
echo %_tail%
Test
测试
> get_tail.cmd "first 1" --flag="other options" --verbose
"first 1" --flag="other options" --verbose
--flag="other options" --verbose
回答by schnaader
You can use SHIFT for this. It removes %1 and shifts all other arguments one lower. This script outputs all the arguments after %2 (so it outputs %3, %4...) until one of them is empty (so it's the last one):
您可以为此使用 SHIFT。它删除 %1 并将所有其他参数降低一位。此脚本输出 %2 之后的所有参数(因此它输出 %3、%4...),直到其中一个为空(因此它是最后一个):
@echo off
SHIFT
SHIFT
:loop
if "%1" == "" goto end
echo %1
SHIFT
goto loop
:end
EDIT: Removed example using %* as this doesn't work - %* always outputs all of the parameters
编辑:删除了使用 %* 的示例,因为这不起作用 - %* 总是输出所有参数
回答by Scott Langham
Building on schnaader's answer, I think this does it if you want everything after %1 concatenated.
基于 schnaader 的回答,我认为如果您想要 %1 连接后的所有内容,就可以这样做。
@echo off
SHIFT
set after1=
:loop
if "%1" == "" goto end
set after1=%after1% %1
SHIFT
goto loop
:end
echo %after1%
回答by BUZA László
Sebi, here's the Syntax! There is a behavior, batch eating the equal signs which is not double quoted, it cause trouble in the scripts above. If you wan't to skip, i've made a modification, based on Raman Zhylich answer and strlen.cmd:
Sebi,这是语法!有一个行为,批量吃不是双引号的等号,它会在上面的脚本中造成麻烦。如果你不想跳过,我已经根据 Raman Zhylich 的回答和 strlen.cmd 进行了修改:
@ECHO OFF
SETLOCAL enableDelayedExpansion
SET _tail=%*
SET "_input="
SET /A _len=0
:again
SET "_param=%1"
SET "_input=%_input%%1"
FOR /L %%i in (0,1,8191) DO IF "!_param:~%%i,1!"=="" (
REM skip param
SET /A _len+=%%i
REM _len can't be use in substring
FOR /L %%j in (!_len!,1,!_len!) DO (
REM skip param separator
SET /A _len+=1
IF "!_tail:~%%j,1!"=="=" (SET "_input=%_input%=" & SHIFT & goto :again)
)
) & goto :next
:next
IF %_len% NEQ 0 SET _tail=!_tail:~%_len%!
ENDLOCAL & SET "_input=%_input%" & SET "_tail=%_tail%"