Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CausticLasagne · Jun 16, 2016 at 09:53 AM · externalbatcharguments

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();
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phxvyper · Jun 17, 2016 at 08:31 PM 0
Share

Did you figure out a solution? Did my answer work for you?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CausticLasagne · Jun 19, 2016 at 03:34 PM 0
Share

Sorry I'm back now. I've had some problems logging into unity forums caused by httpseverywhere. Now. Where were we up to?

avatar image CausticLasagne · Jun 19, 2016 at 03:38 PM 0
Share

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.

avatar image phxvyper CausticLasagne · Jun 20, 2016 at 09:47 AM 0
Share

I found this post on Stack Overflow that may be of help:

http://stackoverflow.com/a/11768260/3007166

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Load external mp3 File without streaming 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges