批处理文件参数处理
Batchh file arguments processing
假设我有一个名为 check.bat 的批处理文件。我 运行 它在 java 使用命令
Runtime.getRuntime().exec("cmd /c start C:\check.bat");
它运行批处理文件没有任何问题。但是当我将参数传递给批处理文件时,
Runtime.getRuntime().exec("cmd /c start C:\check.bat arg1 arg2 arg3 arg4");
我想在 check.bat 中访问这些参数
我知道 %* 得到了我所有的论据。但我想要的是除了最后一个参数之外的所有参数作为单个变量。
批处理文件非常新。请帮忙。
通常,您可以将前三个参数放在引号中,例如 check.bat "arg1 arg2 arg3" arg4
由于这是在 Java 中,您应该能够将一些引号转义到 exec 命令中,例如 Runtime.getRuntime().exec("cmd /c start C:\check.bat \"arg1 arg2 arg3\" arg4");
如果由于某种原因不起作用,您始终可以批量获取四个参数,然后在批处理脚本中对它们执行任何操作。
@echo off
set first_three="%1 %2 %3"
set last_one=%4
SomethingDark 建议的第一种方法(在调用批处理文件之前组合参数)可能是最好的方法,但如果您无法使用它,以下方法可能会有所帮助(您可能需要尝试一下,如果您参数包含对 Windows) 有特殊意义的字符:
@echo off
setlocal
set ALLBUT1=
if "%~2" == "" goto :gotthem
set ALLBUT1=%1
:loop
shift
if "%~2" == "" goto :gotthem
set "ALLBUT1=%ALLBUT1% %1"
goto :loop
:gotthem
set "LAST=%1"
echo All-but-one:%ALLBUT1%:
echo Last:%LAST%:
给出:
S:\>arg one two three
All-but-one:one two:
Last:three:
S:\>arg one two three four
All-but-one:one two three:
Last:four:
假设我有一个名为 check.bat 的批处理文件。我 运行 它在 java 使用命令
Runtime.getRuntime().exec("cmd /c start C:\check.bat");
它运行批处理文件没有任何问题。但是当我将参数传递给批处理文件时,
Runtime.getRuntime().exec("cmd /c start C:\check.bat arg1 arg2 arg3 arg4");
我想在 check.bat 中访问这些参数 我知道 %* 得到了我所有的论据。但我想要的是除了最后一个参数之外的所有参数作为单个变量。 批处理文件非常新。请帮忙。
通常,您可以将前三个参数放在引号中,例如 check.bat "arg1 arg2 arg3" arg4
由于这是在 Java 中,您应该能够将一些引号转义到 exec 命令中,例如 Runtime.getRuntime().exec("cmd /c start C:\check.bat \"arg1 arg2 arg3\" arg4");
如果由于某种原因不起作用,您始终可以批量获取四个参数,然后在批处理脚本中对它们执行任何操作。
@echo off
set first_three="%1 %2 %3"
set last_one=%4
SomethingDark 建议的第一种方法(在调用批处理文件之前组合参数)可能是最好的方法,但如果您无法使用它,以下方法可能会有所帮助(您可能需要尝试一下,如果您参数包含对 Windows) 有特殊意义的字符:
@echo off
setlocal
set ALLBUT1=
if "%~2" == "" goto :gotthem
set ALLBUT1=%1
:loop
shift
if "%~2" == "" goto :gotthem
set "ALLBUT1=%ALLBUT1% %1"
goto :loop
:gotthem
set "LAST=%1"
echo All-but-one:%ALLBUT1%:
echo Last:%LAST%:
给出:
S:\>arg one two three
All-but-one:one two:
Last:three:
S:\>arg one two three four
All-but-one:one two three:
Last:four: