title: How to run nested batch file programatically using C#
body: <p>I am trying to execute a batch  through my Client/server socket program. Inside the main batch file, it calls other batch files . All batch files exist in the same folder. The Server program application is in exist another folder (E:\Apps)  and running it as windows  service.</p>
  <p>Main batch file Path :</p>
  <blockquote>
  <p>E:\MyApp\UAT\guest\mytasks\Main.bat</p>
  </blockquote>
  <p>Inside the main batch file below calls are made</p>
  <blockquote>
  <p>call E:\MyApp\UAT\guest\mytasks\stop.bat</p>
  <p>call %mytasks%\start.bat</p>
  </blockquote>
  <p>When I execute the Main.bat file, my program returns the below error and not the executing  rest of the code in the Main.bat file.</p>
  <blockquote>
  <p>'.\stop.bat' is not recognized as an internal or external command,</p>
  </blockquote>
  <p>Function call</p>
  <blockquote>
  <p>ExecuteShellCommand(&quot;: E:\MyApp\UAT\guest\mytasks\Main.bat&quot;, &quot;E:\&quot;, ref Output, ref Error);</p>
  </blockquote>
  <pre><code>private static void ExecuteShellCommand(string _FileToExecute, string _CommandLine, ref string _outputMessage, ref string _errorMessage)
  {
  
  // Set process variable
  // Provides access to local and remote processes and enables you to start and stop local system processes.
  System.Diagnostics.Process eodProcess = null;
  try
  {
      eodProcess = new System.Diagnostics.Process();
  
      // invokes the cmd process specifying the command to be executed.
      string _CMDProcess = string.Format(System.Globalization.CultureInfo.InvariantCulture, @&quot;{0}\cmd.exe&quot;, new object[] { Environment.SystemDirectory });
  
      // pass executing file to cmd (Windows command interpreter) as a arguments
      // /C tells cmd that we want it to execute the command that follows, and then exit.
      string _Arguments = string.Format(System.Globalization.CultureInfo.InvariantCulture, &quot;/C {0}&quot;, new object[] { _FileToExecute });
  
      // pass any command line parameters for execution
      if (_CommandLine != null &amp;&amp; _CommandLine.Length &gt; 0)
      {
          _Arguments += string.Format(System.Globalization.CultureInfo.InvariantCulture, &quot; {0}&quot;, new object[] { _CommandLine, System.Globalization.CultureInfo.InvariantCulture });
      }
  
      // Specifies a set of values used when starting a process.
      System.Diagnostics.ProcessStartInfo eodProcessStartInfo = new System.Diagnostics.ProcessStartInfo(_CMDProcess, _Arguments);
      // sets a value indicating not to start the process in a new window. 
      eodProcessStartInfo.CreateNoWindow = true;
      // sets a value indicating not to use the operating system shell to start the process. 
      eodProcessStartInfo.UseShellExecute = false;
      // sets a value that indicates the output/input/error of an application is written to the Process.
      eodProcessStartInfo.RedirectStandardOutput = true;
      eodProcessStartInfo.RedirectStandardInput = true;
      eodProcessStartInfo.RedirectStandardError = true;
      eodProcess.StartInfo = eodProcessStartInfo;
  
      // Starts a process resource and associates it with a Process component.
      eodProcess.Start();
  
      // Instructs the Process component to wait indefinitely for the associated process to exit.
      _errorMessage = eodProcess.StandardError.ReadToEnd();
      eodProcess.WaitForExit();
  
  
      // Instructs the Process component to wait indefinitely for the associated process to exit.
      _outputMessage = eodProcess.StandardOutput.ReadToEnd();
      eodProcess.WaitForExit();
  
  }
  
  catch (Exception _Exception)
  {
      // Error
      Console.WriteLine(&quot;Exception caught in process: {0}&quot;, _Exception.ToString());
  }
  finally
  {
      // close process and do cleanup
      eodProcess.Close();
      eodProcess.Dispose();
      eodProcess = null;
  }
  </code></pre>
  <p>}</p>