Batch File Path – How to Call a Batch File and Run Using Its Own Path/Directory

batchbatch-filewindowswindows-command-prompt

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

Best Answer

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