Chaining batch files from within a C# application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FireImp
    New Member
    • Aug 2008
    • 3

    Chaining batch files from within a C# application

    So I've read a lot of post about how to run a batch file from within the C# program. And I followed the instructions with a few alterations to fit my needs. First I am using cmd.exe to actually run the script because I want to take advantage of the /K option, so that the log will be onscreen. And second the batch file must be able to call other batch files in a cascading effect. Which it isn't able to do.

    So when I run the follow function, with
    pathToScripts ="C:\\WORKINGDI RECTORY"
    batchfiles ="mybatchfile.b at"

    Code:
                    
    //----------------------------------------------------------------------------------------------------------
    // ExecuteScript - runs a batch file using cmd.exe with a /k (keep on screen) 
    // parameter.
    //----------------------------------------------------------------------------------------------------------
    public static void ExecuteScript(string pathToScripts, string batchfile )
    {
          try
          {
               string sCmd = "C:\\WINDOWS\\system32\\cmd.exe";
               string sTPath = Path.Combine(pathToScripts, batchfile);
               ProcessStartInfo psi = new ProcessStartInfo(sCmd);
    
               psi.WorkingDirectory = InQuotes(pathToScripts);
               psi.Arguments = " /T:2F /D /K " + InQuotes(sTPath);
               psi.UseShellExecute = true;
    
               Process cmd = new Process();
               cmd.StartInfo = psi;
                if (!cmd.Start())
                {
                        MessageBox.Show("Error", MessageBoxButtons.OK);
                }
          }
          catch (Exception e)
          {
                MessageBox.Show(e.ToString(), " ", MessageBoxButtons.OK);
          }
    }
    I get this error:

    'mybatchfile_ca lled1.bat' is not recognized as an internal or external command,
    operable program or batch file.
    'mybatchfile_ca lled2.bat' is not recognized as an internal or external command,
    operable program or batch file.
    C:\AppPath\MyAp p\bin\Release>
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    That sounds like the batchfile is not created correctly.
    It is looking for those other batch files in the working directory and they do not appear to be with it.
    You will need to either but all the batch files in the same directory, or modify them to point to the fully qualified path where they exist.

    Comment

    • FireImp
      New Member
      • Aug 2008
      • 3

      #3
      The batch files works fine if you click on it. Or if you create a shortcut to it. Both mybatchfile_cal led1.bat, and mybatchfile_cal led1.bat are located in the same directory provided by pathToScripts.

      I really believe that C# should be able to able to emulate the functionality and behavior of a shortcut.
      Last edited by FireImp; Aug 25 '08, 09:52 PM. Reason: clarification

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        They work fine when you click on them or when you run a shortcut to them because the working directory is set to the location where they exist. Unless you set the working directory of the process, you will not see the same result.
        The fault is not C#s, you are just missing some required code for it.

        edit: I think, will check
        editedit: Yup, there is a .WorkingDirecto ry on the StartInfo object that you should be setting.

        Comment

        • DonBytes
          New Member
          • Aug 2008
          • 25

          #5
          Originally posted by Plater
          editedit: Yup, there is a .WorkingDirecto ry on the StartInfo object that you should be setting.
          According to row 13 of the posted code he does set it though. However he sets it using this:
          Code:
          psi.WorkingDirectory = InQuotes(pathToScripts);
          WorkingDirector y of the StartInfo object shouldn't have the path quoted, it'll default to whatever folder the parent process had when it can't find a path like "C:\Windows ". Here's a working sample without the try catch bit though:
          Code:
          //----------------------------------------------------------------------------------------------------------
          		// ExecuteScript - runs a batch file using cmd.exe with a /k (keep on screen) 
          		// parameter.
          		//----------------------------------------------------------------------------------------------------------
          		public static void ExecuteScript(string pathToScripts, string batchfile)
          		{
          			String sCmd = "C:\\WINDOWS\\system32\\cmd.exe";
          			String sTPath = Path.Combine(pathToScripts, batchfile);
          
          			using (Process cmd = new Process())
          			{
          				cmd.StartInfo.FileName = sCmd;
          				cmd.StartInfo.WorkingDirectory = pathToScripts;
          				cmd.StartInfo.Arguments = String.Format(" /T:2F /D /K \"{0}\"", sTPath);
          				cmd.StartInfo.UseShellExecute = true;
          
          				cmd.Start();
          			}
          		}

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by DonBytes
            According to row 13 of the posted code he does set it though.
            I swear I looked at it three times to make sure it wasn't being done, and yet, there it is. Hmm.
            I am not sure then, I've never had any trouble with the process object in .NET

            Comment

            • FireImp
              New Member
              • Aug 2008
              • 3

              #7
              Wow thanks!!! All that pain and suffering because I was putting the working directory in quotes.

              Comment

              • DonBytes
                New Member
                • Aug 2008
                • 25

                #8
                Yeah, C# can handle strings with spaces whereas arguments can not, but when chaining batch files it's easy to get lost on where you need to quote and where you do not.

                Also, looking through it again, the space first in arguments is not needed as it'll be separated anyway.

                Comment

                Working...