2015-01-20 3 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를" 제대로 작성해야하지만, 나는 그것을 할 방법을 잘 모르겠어요 자바에 대한 그래서 조금 알고 : 내가있어.

+2

당신을 "% f"를'\ "% f \"로 작성하십시오. – Titus

+0

Java (및 C 유래 구문이있는 다른 언어)에서 문자열 이스케이프에 대해 얼마나 알고 있습니까? – immibis

+0

@ immibis : 누구의 제로. 많은 VBA를 일하면서 자바에 도전 해보십시오. – TechnoWolf

답변

0

당신은 명령 줄을 실행 Runtime.getRuntime() 간부를 ("...")을 사용할 수 있으며, 예를 들어, 하나 개 이상의 명령을 실행하기 위해 "& &"를 사용할 수 있습니다.

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

흠 좋습니다. Runtime.getRuntime(). exec ("cmd/c \"cd C : \ Users \ Name \ Desktop \ Reports \ Scripts && (* .txt)의 % f는 \ "% f \"를 입력하십시오. >> Export.txt \ ""); 그 전후에 필요한 것이 있습니까? – TechnoWolf