Command line program runs fine from command line but fails when run from Unity

I have a command line executable I wrote that essentially executes a batch file. This batch file makes a call to another executable and redirects its output to a text file called “out.txt”. when its done, my program then reads that file and prints it to standard output (printf). When run from the command line, this works just fine. This is under windows using cmd.exe.

However, when I run this executable from my Unity app, the “out.txt” file never gets created so nothing gets printed and Unity captures no output from the program. Here is how I’m invoking my command line program:

        try
        {
        ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                FileName = Application.persistentDataPath + "/fetchrefdata.exe",
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
                Arguments = refTemplateString
            };
            Process myProcess = new Process
            {
                StartInfo = startInfo
            };

            myProcess.Start();
            string output = myProcess.StandardOutput.ReadToEnd();
            UnityEngine.Debug.Log(output);
            myProcess.WaitForExit();
            int i = 0;
        }

        catch (Exception e)
        {
            print(e);
        }

Now, I know the command line program is getting executed because changing it to just print “Hello World” works and Unity captures that string. But when running the regular command line program, the redirect in the batch file it calls never happens even when given an explicit path for the redirect output file. Again, this command line program works flawlessly when manually run from the command line.

The batch file that gets called by the command line program is essentially just this:

ECHO OFF
reftool.exe [a bunch of arguments] > out.txt

Can anyone shed light on this issue? It’s driving me bonkers.

Some things to look at:

  • wrong working directory
  • wrong path to find executable
  • execution permissions
  • pathing (using forward vs backward slashes)
  • escaping of arguments and/or path files

There might be utilities you can get to observe application spawning faults generated by the OS too.

If I create a dummy “out.txt” file, the tool called from Unity prints it out and Unity captures it, so a wrong working directory for the tool invoked in the batch file seems pretty plausible. I will look at that next. Thanks muchly. I’ll post back here with results once I’ve recovered from four and a half hours of stabs in the dark. :slight_smile:

I’m actually wondering if the system is looking in the wrong place for the executable tool being run in the batch file. I’ll check that, too.

Update: I tried using an explicit path for both the tool being run in the batch file and for the redirected output file “out.txt”. Neither of those changes made any difference. Runs fine from command line, no “out.txt” file generated when run from Unity. Sigh.

The batch file tool wants forward slashes and cmd.exe doesn’t seem to have an issue with that (somewhat surprisingly). If it’s an issue with forward/back slashes, that may be a nest of vipers I’m dreading having to deal with. :frowning:

OK, I’ve solved the issue. It was the working directory, the first thing you suggested to check and what really should have been the first place I looked. So… facepalm. Thanks muchly for your assistance. :slight_smile:

You’re welcome muchly and I’m happy to hear that you’re back on track!