tags:

views:

43

answers:

3

Hi

I have a problem calling a batch file from another batch file when trying to run everything by using Process.Start. Basically I call the execution of a batch file from my c# program that looks like this:


call include.bat  

//execute the rest of the batch file here  

The include.bat file sets up paths and can be used by a number of other batch files. When I run the Process.Start sometimes this works and sometimes I get ERROR: cannot find include.bat. First of all any idea why this happens? And ideas on how to fix this from the batch file?

A: 

First thing I'd try is to use full path information in the call statement for include.bat. If that fixes it, you probably are just not running the batch file from the proper location. I'm sure there's a "working directory" capability in C#, I'm just not sure what it is.

PatrickV
A: 

Do you set ProcessStartInfo.WorkingDirectory ( http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx ) on the ProcessStartInfo that you pass to Process.Start?

Since include.bat sometimes cannot be found, working directory may be wrong (not the folder where include.bat is located).

Andreas Paulsson
I must admit I don't do that. Is there any way to fix it at least temporarily from the batch file?
Dimitris
Use cd %~dp0 as stated by Botz3000 above.
Andreas Paulsson
thanks for that
Dimitris
+2  A: 

To switch to the directory your batch file is located in, use this:

cd %~dp0

I do this in almost all of my batch scripts. That way relative paths should always work.

Botz3000
thanks for that
Dimitris
You can mark it as answer if it solved your problem. :)
Botz3000