2015-01-20 159 views
0

Noobish问题我确定,但我试图打开命令提示符,切换到一个目录,然后运行第二个命令。我有:Java - 打开命令提示符,运行两个命令

try { 
// Execute command 
String command = "cmd /c start cmd.exe"; 
Process child = Runtime.getRuntime().exec(command); 

// Get output stream to write from it 
OutputStream out = child.getOutputStream(); 

out.write("cd C:\Users\Me\Desktop\Reports\Scripts /r/n".getBytes()); 
out.flush(); 
out.write("for %f in (*.txt) do type "%f" >> Export.txt /r/n".getBytes()); 
out.close(); 
} catch (IOException e) { 
} 

我无知的猜测是“%F”在第二个命令需要正确写的,但我知道的太少了有关Java我不知道该怎么做。

+2

你需要转义引号,把'%f'写成'\'%f'' – Titus 2015-01-20 23:51:06

+0

你对Java中的字符串转义(以及其他C语言的语言)有多少了解? – immibis 2015-01-20 23:53:23

+0

@ immibis:谁多零。为工作做许多VBA并尝试冒险进入Java。 – TechnoWolf 2015-01-20 23:55:10

回答

0

您可以使用调用Runtime.getRuntime()EXEC( “... ”)运行命令行,你可以使用“ & &” 执行多个命令,如:

Runtime.getRuntime().exec("cmd /c \"start something.bat && start somethingelse.bat \""); 
+0

嗯好吧我尝试了Runtime.getRuntime()。exec(“cmd/c \”cd C:\ Users \ Name \ Desktop \ Reports \ Scripts && for%f in(* .txt)do type \“%f \” >> Export.txt \“”);在此之前或之后我需要什么吗? – TechnoWolf 2015-01-21 00:06:31