Leo*_*nid 5 batch-file command-line-arguments
我想使用传递给批处理文件的前2个参数,然后将其他所有内容附加到批处理文件中执行的命令.
有一个简单的方法吗?
// batch file
command -Duser=%1 -Dpwd=%2 {? how to pass all remaining arguments on the command line}
Run Code Online (Sandbox Code Playgroud)
批处理将像这样执行
batch.bat USR PWD -Dargument1=1 -Dargument2=2
Run Code Online (Sandbox Code Playgroud)
实际上,我希望执行以下内容
command -Duser=USR -Dpwd=PWD -Dargument1=1 -Dargument2=2
Run Code Online (Sandbox Code Playgroud)
该for命令应该给你你想要的:
for /f "tokens=1,2*" %%a in ("%*") do echo command -Duser=%%a -Dpwd=%%b %%c
Run Code Online (Sandbox Code Playgroud)
for来自( )的帮助for /?:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
将解析 myfile.txt 中的每一行,忽略以分号开头的行,将每行的第二个和第三个标记传递到 for 正文,标记以逗号和/或空格分隔。 请注意 for body 语句引用 %i 来获取第二个标记,%j 来获取第三个标记,%k 来获取第三个标记之后的所有剩余标记。 对于包含空格的文件名,需要用双引号将文件名引起来。为了以这种方式使用双引号,您还需要使用 usebackq 选项,否则双引号将被解释为定义要解析的文字字符串。
因此,在我的解决方案中,%%a第一个参数的值是否传递给脚本,%%b第二个参数的值以及%%c其余参数。
当我batch.bat USR PWD -Dargument1=1 -Dargument2=2从命令提示符运行时,我得到:
command -Duser=USR -Dpwd=PWD -Dargument1=1 -Dargument2=2
Run Code Online (Sandbox Code Playgroud)
删除echo后面的 来do使用参数调用命令。