3

I got two batch-files. The first is started through C# code:

System.Diagnostics.Process.Start(System.Environment.CurrentDirectory + "\\Files\\myBat.bat",  parameters + " > output.txt");

The second bat is started inside the first one:

%MY_PATH%\secondBat.bat

First one runs as supposed, but execution of second (inside of first bat) is ignored. It all works until this Point.

It DOES work if started manually via cmd.exe It ONLY does not work if started via C#.

Thanks for help

Edit: This is the Output:

C:\TheProg\XXXX\bin\Debug>C:\TheProg\XXXX\bin\Debug\secondBat.bat

It just doesn´t execute

Best_Where_Gives
  • 481
  • 2
  • 22
  • The same effect if you run using "cmd /C"? – Artem Razin Jun 16 '14 at 11:23
  • Check this example of how to launch a batch file - seems MUCH more verbose and may "just work" though I cannot offer any more advice than that! http://stackoverflow.com/a/5519517/1370442 – Luke Baughan Jun 16 '14 at 11:23
  • 1
    Unfortunately, this is the standard-Scenario which I am not looking for. It has the exact same effect as my code. @Artem Razin: Yes – Best_Where_Gives Jun 16 '14 at 11:39

2 Answers2

0

try to start the batch file using cmd.exe and provide the path including the arguments for the batch file, as argument to System.Diagnostics.Process.Start

System.Diagnostics.Process.Start("cmd.exe" , System.Environment.CurrentDirectory + "\\Files\\myBat.bat" +  parameters + " > output.txt");
AcidJunkie
  • 1,878
  • 18
  • 21
  • 1
    Hello, I already tried that. It seems to ignore the argument for cmd.exe. It just opens cmd, and then cmd does nothing. – Best_Where_Gives Jun 16 '14 at 11:35
  • Try to add the `/k` parameter which prevents the command prompt from being closed. That might contain an error message which now will be visible – AcidJunkie Jun 16 '14 at 11:38
  • 1
    AcidJunkie, this is unneccessary because, as I said, the window DOES already stay open this way. There is no error message, just the cmd-prompt waiting for input – Best_Where_Gives Jun 16 '14 at 11:41
  • Okay. Has any of the statements in the batch file executed? What is the content of the first batch file? – AcidJunkie Jun 16 '14 at 11:43
  • 1
    Yes, all of them. It´s a bit Special: It starts a .exe two times (works), then it copies two files (works) and THEN should "just" start the second .bat Edit: I tested what happens, if I put the .bat BEFORE the two copy-commands... it just skips it. It does not halt or abort, it simply ignores the command... – Best_Where_Gives Jun 16 '14 at 11:46
  • 1
    Thanks guys, I got it now... It was actually a simple path-finding-problem. The standard-Folder in documents is named "Visual Studio 2010" with SPACES inbetween (classic). So just moving the folder to C:\Test was the solution. I´m a Little embarrassed now... For some reason I can not yet post answers... – Best_Where_Gives Jun 16 '14 at 11:58
  • @Franky_ley glad you could find the solution – AndreySarafanov Jun 16 '14 at 12:00
0

I've tried to reproduce your problem. I created test .bat files in C:\simplebats

bat1.bat:

@echo off
echo "bat1 started"
C:\simplebats\bat2.bat

bat2.bat:

echo "bat2 started"

I run bat1 from visual studio the way you did:

System.Diagnostics.Process.Start("C:\\simplebats\\bat1.bat", " > output.txt");

After i run it an output.txt file is created in my Debug folder (not in simplebats) and it has the expected two lines:

"bat1 started"
"bat2 started"

Are you absolutely sure that the second .bat file is not fired in your situation? Maybe you can't see it being fired because the output file is created in your Debug folder? That is the only thing that comes to my mind..

AndreySarafanov
  • 804
  • 5
  • 20
  • 2
    It was something different, but your very first sentence gave me the "click" It was actually a simple path-finding-problem. The standard-Folder in documents is named "Visual Studio 2010" with SPACES inbetween (classic). So just moving the folder to C:\Test was the solution. I´m a Little embarrassed now... For some reason I can not yet post answers... – Best_Where_Gives Jun 16 '14 at 11:56