简体   繁体   English

如何在Windows批处理文件中获取其余参数?

[英]How to get the rest of arguments in windows batch file?

I know I can get first argument with %0, second with %1, and so on.. And I can also get all arguments with %*. 我知道我可以用%0得到第一个参数,用%1得到第二个,依此类推。我也可以用%*获得所有参数。

Can I get all arguments from the second arguments? 我可以从第二个参数得到所有参数吗? For example, if I run 例如,如果我跑

foo.bat bar1 bar2 bar3 bar4

How can I get only bar2 bar3 bar4 ? 我怎么才能得到bar2 bar3 bar4

@ECHO OFF
SETLOCAL
SET allargs=%*
IF NOT DEFINED allargs echo no args provided&GOTO :EOF 
SET arg1=%1
CALL SET someargs=%%allargs:*%1=%%
ECHO allargs  %allargs%
ECHO arg1     %arg1%
ECHO someargs %someargs%

This will leave SOMEARGS with at least one leading separator (if it is set) 这将使SOMEARGS至少有一个前导分隔符(如果已设置)

With SHIFT command. 使用SHIFT命令。 But with every shift you'll lose the first. 但是每次换班都会失去第一名。 This will not change the %* but you'll be able to get all argument ,but the first: 这不会改变%*但你将能够获得所有参数,但第一个:

@echo off
shift

set "arg_line= "
:parse_args
if "%~1" NEQ "" (
 arg_line=%argline% "%~1"
 goto :parse_args
)

now you'll have all arguments but the first stored in %arg_line% 现在你将拥有所有参数,但第一个存储在%arg_line%

You need to use SHIFT . 您需要使用SHIFT It moves the apparent position of the parameters, then %* will get all parameters from the position shifted to. 它移动参数的视在位置,然后%*将从位置移动到所有参数。 You should get the first parameters before using SHIFT . 在使用SHIFT之前,您应该获得第一个参数。

More information on SHIFT . 有关 SHIFT 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM