简体   繁体   中英

Java - Open command prompt, run two commands

Noobish question I'm sure, but I'm trying to open a command prompt, switch to a directory, then run a second command. I've got:

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) {
}

My ignorant guess is the "%f" in the second command needs to be written properly, but I know so little about java I'm not sure how to do it.

您可以使用Runtime.getRuntime()。exec(“ ...”)运行命令行,也可以使用“ &&”执行多个命令,例如:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM