locked
Capture the response from a batch file. RRS feed

  • Question

  • Hi

    I am succesfully able to invoke the batch file and run it. But i am not able to capture the response, whether the file commands have been executed properly or not. This is used to dump data to a mainframe server. Below is the code for doing this.

    //dump current csv file to be uploaded to app data folder

     

    string CompleteLocalPath = Server.MapPath("~/App_Data/FTPMainframe_" + currentcsvFile);
    StreamWriter sw = new StreamWriter(CompleteLocalPath);
    sw.Write(strSource.ToString());//strSource is the string being passed to thi method
    sw.Close();
    //generate dynamic batch file with above file name as part of it
    StringBuilder sbbat = new StringBuilder();
    sbbat.Append(
    "@ftp -i -s:\"%~f0\"&GOTO:EOF\r\n");
    sbbat.Append(
    "open MyServer.COM\r\n");
    sbbat.Append(System.Configuration.
    ConfigurationManager.AppSettings["FTPUser"].ToString() + "\r\n");
    sbbat.Append(System.Configuration.
    ConfigurationManager.AppSettings["FTPPassword"].ToString() + "\r\n");
    sbbat.Append(
    "QUOTE SITE LRECL=348 BLKSIZE=5916 RECFM=FB\r\n");
    sbbat.Append(
    "put " + CompleteLocalPath + " DATASETNAME\r\n");
    sbbat.Append(
    "bye\r\n");
    //write new batch fil to app_data
    StreamWriter swbat = new StreamWriter(CompleteBatchFilePath);
    swbat.Write(sbbat.ToString());
    swbat.Close();
    //execute newly generated bat file from command
    Process myCmd = null;
    ProcessStartInfo qOptions = new ProcessStartInfo(@"cmd.exe", @"/k " + CompleteBatchFilePath);
    qOptions.WindowStyle =
    ProcessWindowStyle.Hidden;
    qOptions.RedirectStandardOutput =
    false; // set to "true" to enable logging
    qOptions.UseShellExecute = true; // set to true to make silent/background
    myCmd = Process.Start(qOptions);

    i tried using qOptions.RedirectStandardOutput = true; and qOptions.RedirectStandardError = true; but couldnot collect the response properly.

    Any help will be greatly appreciated.

    Thanks
    Vicky

    • Moved by John Saunders Tuesday, February 9, 2010 7:51 PM Random .NET Question (From:ASMX Web Services and XML Serialization)
    Tuesday, February 9, 2010 9:14 AM

Answers

  • Hi, Vicky84:
         Please check the following code. This code execute a batch.cmd file and store the ouput in a String. You can put your command in the batch.cmd and manipulate the output via the String variable.
      

    using System;
    using System.Diagnostics;
    
    namespace GetBatchCmdOutput
    {
        class Program
        {
            static void Main(string[] args)
            {
                String batch_file_path = @"d:\batch.cmd";
                Process myCmd = null;
                ProcessStartInfo psi = new ProcessStartInfo(batch_file_path);
                psi.RedirectStandardOutput = true;
                psi.UseShellExecute = false;
                myCmd = Process.Start(psi);
                String batch_output = myCmd.StandardOutput.ReadToEnd();
                Console.WriteLine(batch_output);
            }
        }
    }
        


            Good luck!


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Proposed as answer by SamAgain Tuesday, February 16, 2010 7:57 AM
    • Marked as answer by eryang Friday, February 19, 2010 3:39 AM
    Tuesday, February 16, 2010 7:55 AM

All replies

  • What you will want to do is to change RedirectStandardOutput to True
    Then just after you start it add this line.

    string output = myCmd.StandardOutput.ReadToEnd()
    This should get the output, of anything that would have been written to the screen.  Now, if you get blank output, you could try modifying the way that you start the application.  Rather than starting cmd.exe and running your batch, just start the .bat file directly.
    Mitchel Sellers, C# MVP - http://www.mitchelsellers.com
    Tuesday, February 9, 2010 4:52 PM
  • Vicky, you posted in the wrong forum. This question has nothing to do with web services or XML serialization.

    I'm going to move it.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, February 9, 2010 7:50 PM
  • Hi, Vicky84:
         Please check the following code. This code execute a batch.cmd file and store the ouput in a String. You can put your command in the batch.cmd and manipulate the output via the String variable.
      

    using System;
    using System.Diagnostics;
    
    namespace GetBatchCmdOutput
    {
        class Program
        {
            static void Main(string[] args)
            {
                String batch_file_path = @"d:\batch.cmd";
                Process myCmd = null;
                ProcessStartInfo psi = new ProcessStartInfo(batch_file_path);
                psi.RedirectStandardOutput = true;
                psi.UseShellExecute = false;
                myCmd = Process.Start(psi);
                String batch_output = myCmd.StandardOutput.ReadToEnd();
                Console.WriteLine(batch_output);
            }
        }
    }
        


            Good luck!


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Proposed as answer by SamAgain Tuesday, February 16, 2010 7:57 AM
    • Marked as answer by eryang Friday, February 19, 2010 3:39 AM
    Tuesday, February 16, 2010 7:55 AM