Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 580 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Include a batch file in a batch file

#1
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:

<pre><code>
call include.bat

//execute the rest of the batch file here

</code></pre>

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?
Reply

#2
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.
Reply

#3
Do you set ProcessStartInfo.WorkingDirectory (

[To see links please register here]

) 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).
Reply

#4
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.
Reply

#5
Before the script, try `CD /D %~dp0`
Reply

#6
I know this is an old question but I thought it would be worth noting that the approach promoted by the accepted answer (i.e. changing the working directory) may not always be appropriate.

**A better *general* approach is to refer to dependencies by full path:**

call "%~dp0include.bat"

(Since %~dp0 already ends with a backslash, we don't need to add another one.)

**Here are some benefits of *not* changing the working directory:**

1. The rest of the batch file can still use the original working directory.
2. The original working directory in the command prompt is preserved, even without "SETLOCAL".
3. If the first batch file is run via a UNC path (such as "\\\\server\share\file.bat"), the full-path call will succeed while [changing the directory (even with "cd /d") will fail][1]. (Using pushd/popd would handle this point, but they have their own set of problems.)

These benefits are particularly important for alias-type batch files, even if they are not as important for the specific situation that motivated this question.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through