2015-03-31 79 views
0

说我有一个名为check.bat的批处理文件。我使用的命令Batchh文件参数处理

Runtime.getRuntime().exec("cmd /c start C:\\check.bat"); 

它运行没有问题的批处理文件,java的运行它。但是,当我将参数传递给该批处理文件等,

调用Runtime.getRuntime()EXEC键。( “CMD/C开始C:\ check.bat ARG1参数3 ARG4”);

我想访问check.bat里面的这些参数 我知道%*得到了我所有的参数。但我想要的是除了最后一个参数以外的所有参数作为单个变量。 非常新的批处理文件。请帮忙。

回答

0

由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: 
+0

非常感谢。工作超细:) – 2015-04-01 06:34:18

0

通常情况下,你就可以通过把它们放在引号像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