How to run programs in the same directory as a Windows batch file?

I have a .bat and .exe file in the same folder. I could not call the .exe from .bat unless I put the full absolute path to it. Is there a way to not specify a path?

+43
windows path relative-path batch-file
Apr 28 '10 at 15:02
source share
3 answers

Try calling .exe with %~dp0 , for example: %~dp0MyProgram.exe .

%0 contains the full path to the .bat file to be called.

~dp says to get the drive and path, including trailing \ .

+98
Apr 28 2018-10-18T00:
source share

I solved this by changing the working directory using pushd at the beginning of the script, and recovery is at the end of the script using popd . Thus, you can always assume that the working directory matches the location of the bat file.

 pushd %~dp0 ProgramInSameFolderAsBat.exe popd 
+9
Apr 05 '16 at 9:39
source share

seems strange? I have never worked on windows, but

Have you tried with. /

./program.exe

really shouldn't be a difference? is it possible that the bat is executed from the context of C: \ Windows, or at some point does your batch make any disk in another directory?

0
Apr 28 '10 at 15:07
source share



All Articles