470,566 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,566 developers. It's quick & easy.

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:\\WORKINGDIRECTORY"
batchfiles ="mybatchfile.bat"

Expand|Select|Wrap|Line Numbers
  1.  
  2. //----------------------------------------------------------------------------------------------------------
  3. // ExecuteScript - runs a batch file using cmd.exe with a /k (keep on screen) 
  4. // parameter.
  5. //----------------------------------------------------------------------------------------------------------
  6. public static void ExecuteScript(string pathToScripts, string batchfile )
  7. {
  8.       try
  9.       {
  10.            string sCmd = "C:\\WINDOWS\\system32\\cmd.exe";
  11.            string sTPath = Path.Combine(pathToScripts, batchfile);
  12.            ProcessStartInfo psi = new ProcessStartInfo(sCmd);
  13.  
  14.            psi.WorkingDirectory = InQuotes(pathToScripts);
  15.            psi.Arguments = " /T:2F /D /K " + InQuotes(sTPath);
  16.            psi.UseShellExecute = true;
  17.  
  18.            Process cmd = new Process();
  19.            cmd.StartInfo = psi;
  20.             if (!cmd.Start())
  21.             {
  22.                     MessageBox.Show("Error", MessageBoxButtons.OK);
  23.             }
  24.       }
  25.       catch (Exception e)
  26.       {
  27.             MessageBox.Show(e.ToString(), " ", MessageBoxButtons.OK);
  28.       }
  29. }
I get this error:

'mybatchfile_called1.bat' is not recognized as an internal or external command,
operable program or batch file.
'mybatchfile_called2.bat' is not recognized as an internal or external command,
operable program or batch file.
C:\AppPath\MyApp\bin\Release>
Aug 22 '08 #1
7 1889
Plater
7,872 Expert 4TB
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.
Aug 25 '08 #2
The batch files works fine if you click on it. Or if you create a shortcut to it. Both mybatchfile_called1.bat, and mybatchfile_called1.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.
Aug 25 '08 #3
Plater
7,872 Expert 4TB
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 .WorkingDirectory on the StartInfo object that you should be setting.
Aug 26 '08 #4
editedit: Yup, there is a .WorkingDirectory 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:
Expand|Select|Wrap|Line Numbers
  1. psi.WorkingDirectory = InQuotes(pathToScripts);
WorkingDirectory 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:
Expand|Select|Wrap|Line Numbers
  1. //----------------------------------------------------------------------------------------------------------
  2.         // ExecuteScript - runs a batch file using cmd.exe with a /k (keep on screen) 
  3.         // parameter.
  4.         //----------------------------------------------------------------------------------------------------------
  5.         public static void ExecuteScript(string pathToScripts, string batchfile)
  6.         {
  7.             String sCmd = "C:\\WINDOWS\\system32\\cmd.exe";
  8.             String sTPath = Path.Combine(pathToScripts, batchfile);
  9.  
  10.             using (Process cmd = new Process())
  11.             {
  12.                 cmd.StartInfo.FileName = sCmd;
  13.                 cmd.StartInfo.WorkingDirectory = pathToScripts;
  14.                 cmd.StartInfo.Arguments = String.Format(" /T:2F /D /K \"{0}\"", sTPath);
  15.                 cmd.StartInfo.UseShellExecute = true;
  16.  
  17.                 cmd.Start();
  18.             }
  19.         }
Aug 26 '08 #5
Plater
7,872 Expert 4TB
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
Aug 26 '08 #6
Wow thanks!!! All that pain and suffering because I was putting the working directory in quotes.
Aug 27 '08 #7
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.
Aug 31 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Charles Neitzel | last post: by
3 posts views Thread by emman_54 | last post: by
1 post views Thread by simon.robin.jackson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.