17

I have a batch files that calls other batch files like this:

e:\foo\master.bat has the content:

call e:\bar\run1.bat 

and e:\bar\run1.bat has the content

app1.exe

the problem is that when I run the master.bat app1.exe will not be executed, because it will expect it to be in the e:\foo directory instead of it being in e:\bar directory

Colyn1337
  • 2,387
  • 2
  • 22
  • 38
Omu
  • 327
  • 2
  • 6
  • 19

2 Answers2

17

You are a bit unclear where app1.exe is located.

If it shares the folder with run1.bat change run1.bat

to either

@Echo off
Pushd "%~dp0"
app1.exe
popd

or

@Echo off
"%~dp0app1.exe"

%0 refers to the currently running batch and the modifier ~dp returns drive and path (with a trailing backslash.)

LotPings
  • 1,015
  • 7
  • 12
  • the `Pushd` method works best for me because, `app1.exe` internally also uses current dir, and using Pushd it will use the dir where `app1.exe` is located, without Pushd (but with the prefix) it will use the `master.bat` location – Omu May 31 '18 at 18:18
  • 1
    With this type of implications in mind, that method was the first in my answer intentionally. Thanks for feedback. – LotPings May 31 '18 at 18:22
4

The answer to your question can be drawn from a similar question on Stack Overflow.

What is the current directory in a batch file?

Using the variables mentioned here, you can update run1.bat to call app1.exe with the following line: %~dp0app1.exe. (The %~dp0 variable includes a trailing slash.) This will tell the batch file to run the executable from the current batch file's location.

SamErde
  • 3,324
  • 3
  • 23
  • 42