2015-01-20 171 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