3

我正在使用 Java ProcessBuilder 将字符串作为参数传递给批处理文件。

ProcessBuilder pb = new ProcessBuilder("batch.bat", "jason mary molly");

批处理文件...

 @ECHO OFF
 SET V1=%1
 ECHO %V1%
 pause

批处理文件输出是这样的(注意双引号):

“杰森玛丽莫莉”

如果我只传入 1 个字符串,则找不到引号!我的问题是,我正在编写一个相当复杂的程序,它要求这 3 个参数周围没有引号,否则程序会将它们视为一个文件名而不是 3 个单独的参数。有没有办法删除这些双引号?

4

2 回答 2

2

尝试:%~1它应该去掉引号

有关详细信息,请键入:for /? | more +121在 cmd 中。以下是报价参考:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

可以组合修饰符以获得复合结果:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line
于 2013-09-26T03:16:46.523 回答
1

String您传递给的每个ProcessBuilder参数都成为您正在执行的命令的参数,因此使用

ProcessBuilder pb = new ProcessBuilder( "batch.bat", "jason mary molly");

意味着您将1 个参数传递给您的命令,而不是三个。

尝试使用...

ProcessBuilder pb = new ProcessBuilder( "batch.bat", "jason", "mary", "molly");

反而

于 2013-09-26T03:21:02.420 回答