- Home /
How to Execute an external batch file with arguments
Hi everyone,
I'm having a small issue at the moment.
Basically I need to execute an external batch file from unity at Runtime.
Currently I'm using cmd.exe to do this, however I also need to pass some arguments to the batch file as commands, not cmd as arguments.
Does anybody know of a clean way I can execute a batch file, with passing on commands, and also getting the output?
Here is what I have so far:
IEnumerator excmd()
{
datapath = Application.persistentDataPath;
percentage.text = "Building Workspace...";
ExecuteCommand("cd "+Application.persistentDataPath + "forge gradlew.bat", "-cleanCache -clean -setupDecompWorkspace --refresh-dependencies");
yield return null;
}
static void ExecuteCommand(string command, string args)
{
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
UnityEngine.Debug.LogError("D>" + e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
UnityEngine.Debug.LogError("E>" + e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
//Console.WriteLine("ExitCode: {0}", process.ExitCode);
process.Close();
}
Did you figure out a solution? Did my answer work for you?
Answer by phxvyper · Jun 16, 2016 at 10:47 AM
There are two ways you can do this with your current code:
make line 11 from
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
to:
var processInfo = new ProcessStartInfo(command, args);
since all you're doing is starting a process.
OR, you can continue using cmd.exe, but include your args like so:
// expected output: cmd /c ""someCommand" args"
var processInfo = new ProcessStartInfo("cmd.exe", String.Format("/c \"\"{0}\" {1}", command, args));
If you're wondering why the quotations are like that in the output: Bash and CMD treat quotations like Programming Languages treat parentheses.
e.g. its grammatically equivalent to:
cmd /c ((someCommand) args)
EDIT: Are you implementing Continuous Integration w/ Gradle? :) You may be able to help me out with an issue I'm experience with my Travis CI - might switch to gradle.
Sorry I'm back now. I've had some problems logging into unity forums caused by httpseverywhere. Now. Where were we up to?
I've been doing some digging around and managed to get the command input and output working, but now the console does not recognize any of the commands I input, even 'help' or '?'.
static void ExecuteCommand()
{
string output = string.Empty;
string error = string.Empty;
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.WorkingDirectory = Application.persistentDataPath + "/forge";
//info.Arguments = ("gradlew.bat - cleanCache - clean - setupDecompWorkspace--refresh - dependencies");
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("help");
//sw.WriteLine("cd " + Application.persistentDataPath + "/forge");
//sw.WriteLine("gradlew.bat - cleanCache - clean - setupDecompWorkspace--refresh - dependencies");
}
}
using (StreamReader streamReader = p.StandardOutput)
{
output = streamReader.ReadToEnd();
}
using (StreamReader streamReader = p.StandardError)
{
error = streamReader.ReadToEnd();
}
UnityEngine.Debug.LogError("out:" + output);
if (!string.IsNullOrEmpty(error))
{
UnityEngine.Debug.LogError("err:" + error);
}
//p.Close();
}
@phxvyper, I'm using gradle to build the $$anonymous$$ecraft modding environment, if that's what you mean. I need to build it via console and also to control the workspace, compiling mods, moving files, etc.
I found this post on Stack Overflow that may be of help:
Your answer

Follow this Question
Related Questions
Newly instantiated object not being batched? 1 Answer
VBScript through C# in Unity 0 Answers
Facebook apprequest problem 2 Answers
How to load objects from a file outside of unity3d 3 Answers