0

I have a batch file which runs perfectly fine if I give this command in the command prompt.

C:\app> C:\app\Process.bat C:\app\files\uploads c:\app\files file2 <- WORKS

So there are just 3 input parameters.

C:\app\files\uploads : the folder location of input files 
c:\app\files         : the output folder
file2                : output file name

If I run the batch file from C:\app folder I see the output file I want to automate the process from a console app which will be scheduled job But running in visual studio debug mode or clicking the exe file does nothing. I don't get any kind of exception either.

What can be wrong - permission or other thing I am doing wrong?

This is the C# code

static void Main(string[] args)
        {
            RunBatchFile(@"C:\app\Process.bat", @"C:\app\files\uploads c:\app\files 123456");
        }

public static string RunBatchFile(string fullPathToBatch, string args)
        {            
            using (var proc = new Process
            {
                StartInfo =
                {
                    Arguments = args,                    
                    FileName = fullPathToBatch,

                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = false,
                    RedirectStandardError = false
                }
            })
            {
                try
                {
                    proc.Start();
                }
                catch (Win32Exception e)
                {
                    if (e.NativeErrorCode == 2)
                    {
                        return "File not found exception";
                    }
                    else if (e.NativeErrorCode == 5)
                    {
                        return "Access Denied Exception";
                    }
                }
            }

            return "OK";
        }
kheya
  • 7,546
  • 20
  • 77
  • 109

1 Answers1

1

2 Issues here:

First problem is you need to execute cmd.exe not the batch file.
Second, you are executing it but you need to wait for the process to complete. Your app is the parent process and because you're not waiting the child process doesn't complete.

You need to issue a WaitForExit():

var process = Process.Start(...);
process.WaitForExit();

Here is what you want to do:

static void ExecuteCommand(string command)
{
    int exitCode;
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;
    process = Process.Start(processInfo);
    process.WaitForExit();
    exitCode = process.ExitCode;
    process.Close();
}

To run your batch file do this from the main():

ExecuteCommand("C:\app\Process.bat C:\app\files\uploads c:\app\files file2");
T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • You saying I can't run a xyz.bat file from C# code? Sounds odd to me – kheya Jan 13 '14 at 04:20
  • no you cannot, a batch file runs inside the process of cmd.exe – T McKeown Jan 13 '14 at 04:21
  • The batch file little complex. I would have to find a solution of calling it by name rather than passing the big complex commands directly. – kheya Jan 13 '14 at 04:57
  • I think you are missing the point, you can execute your batch file but you do that by creating a method like I have posted in my answer. The command argument to the ExecuteCommand function would be the name of your batch file. – T McKeown Jan 13 '14 at 13:58
  • I have modified my answer to tell you what to do. Not sure why you are not understanding this answer. – T McKeown Jan 13 '14 at 14:01
  • If I run the exe from a command prompt. I have the command hardcoded in the app. C:\DEV\QueueProcessor>QueueProcessor.exe output>>(none) error>>(none) ExitCode: 0 – kheya Jan 13 '14 at 18:48
  • This is from the debug window command="C:\\files\\ProcessVideos.bat C:\files\uploads\52d272400d4aff34f07773ae 123" After passing this to the ExecuteCommand it does nothing – kheya Jan 13 '14 at 18:53
  • I am just running the C:\DEV\QueueProcessor>QueueProcessor.exe. I see the files getting generated but the process never exits. I don't get the prompt back. I have to press ctrl-C to get the command prompt – kheya Jan 13 '14 at 19:05
  • I got it working. It was hanging because your code has a bug (at least for me). Here is the solution http://stackoverflow.com/questions/139593/processstartinfo-hanging-on-waitforexit-why – kheya Jan 13 '14 at 19:21
  • Ok, i will modify my answer. Basically you don't need to do the ReadToEnd() calls if you just trying to execute and not evaluate standardOut – T McKeown Jan 13 '14 at 19:23