1

I am writing the batch file and executing it through C# program.

Writing Batch file :

I will get the Path, Executable name and arguments from app.config and write them to a batch file.

Executing Batch file :

Once I write the batch file I pass the file name to below function which executes the batch file to launches an application.

Problem :

My program will write a lot of batch files which are executed immediately after each and every file is written. I find that, some times the applications are not started which means that batch files are not executed. I didn't even get any error messages or prompts for this failure of batch file execution.

Expected solution :

Any problem in executing the batch file, I should be able to log it or prompt an error.

Code that executes Batch File :

            System.Diagnostics.ProcessStartInfo procinfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            procinfo.UseShellExecute = false;
            procinfo.RedirectStandardError = true;
            procinfo.RedirectStandardInput = true;
            procinfo.RedirectStandardOutput = true;

            System.Diagnostics.Process process = System.Diagnostics.Process.Start(procinfo);

            System.IO.StreamReader stream = System.IO.File.OpenText(BatchPath + LatestFileName);
            System.IO.StreamReader sroutput = process.StandardOutput;
            System.IO.StreamWriter srinput = process.StandardInput;

            while (stream.Peek() != -1)
            {
                srinput.WriteLine(stream.ReadLine()); 
            }

            Log.Flow_writeToLogFile("Executed .Bat file : " + LatestFileName);
            stream.Close();
            process.Close();
            srinput.Close();
            sroutput.Close();

1 Answer 1

2

I'm not sure where your problem lies specifically but I've had no problems with the following code:

using (FileStream file = new FileStream("xyz.cmd", FileMode.Create)) {
    using (StreamWriter sw = new StreamWriter(file)) {
        sw.Write("@echo ====================\n");
        sw.Close();
    }
}

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "xyz.cmd";
//p.StartInfo.RedirectStandardOutput = true;
p.Start();
//String s = p.StandardOutput.ReadLine();
//while (s != null) {
//    MessageBox.Show(s);
//    s = p.StandardOutput.ReadLine();
//}
p.WaitForExit();

Obviously that's been cut down a bit for the purposes of hiding my "secret sauce" but that's code currently being used in production without issues.

I do have one question. Why don't you execute the cmd file directly rather than running cmd.exe?

Probably the first thing I'd do is to print out the BatchPath + LatestFileName value to see if you're creating any weirdly named files which would prevent cmd.exe from running them.

Sign up to request clarification or add additional context in comments.

2 Comments

I create a .Bat file and execute in cmd.exe. In this case where is the XYZ.cmd ? Am i missing something here ? Once i write a batchfile i name it as "string.Format("{0:dd-MM-yyyy[HH-mm-ss-fffff]}", dt);" to make each batch file name unique. Help...Thanks.
@Anuya, you're running cmd.exe and passing each line of the batch file to its standard input. That seems a convoluted way to do it. Since the batch file is already on disk, I'd just run that instead of cmd.exe, then you don't have to worry about sending the individual lines to it. Just replace xyz.cmd in my given code with your own batch file name (based on the date/time). My using bit is just there to show you how I created the batch file. The actual running starts with the Process p =... bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.