问题
I have a server application running called TerrariaServer.exe and I want to be able to send it commands with separate batch file. TerrariaServer.exe is a program running as a command line. How could I "feed" it a command such as "save" and "exit"? The answer might be pipes, but I'm not too sure. Here is kinda what I executed in a batch file while TerrariaServer.exe was running...
@echo off
echo save | TerrariaServer.exe
echo exit | TerrariaServer.exe
After that, nothing happen. I don't know if you need to know this but this is a video game server and the "save/exit" commands come with it.
回答1:
Eep, use type for multiline input!
echo save | TerrariaServer.exe
will open up TerrariaServer.exe and send "save" as input to it.
echo exit | TerrariaServer.exe
will open up TerrariaServer.exe and send "exit" as input to it.
See the problem yet? :P
You're executing TerrariaServer.exe twice!
You should use the command type. You can type a text document into the input of an executable. First, let's create this text document (or any file for that matter; the extension doesn't matter!)
echo save>somefile.txt
echo exit>somefile.txt
Now let's type this into TerrariaServer.exe...
type somefile.txt | TerrariaServer.exe
Maybe delete somefile.txt after we're done?
del somefile.txt
Hope that cleared things up! :)
来源:https://stackoverflow.com/questions/13832786/how-do-i-send-commands-to-an-exe-running-through-the-command-line-with-batch