开发者

How to execute programs in the same directory as the windows batch file?

开发者 https://www.devze.com 2022-12-28 17:16 出处:网络
I have in the same folder a .bat and a .exe file. I couldn\'t call the .exe file from the 开发者_如何学C.bat unless I put the full absolute path to it.

I have in the same folder a .bat and a .exe file. I couldn't call the .exe file from the 开发者_如何学C.bat unless I put the full absolute path to it. Is there a way to don't specify the path?


Try calling the .exe with %~dp0, like this: %~dp0MyProgram.exe.

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

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


I solved this by changing the working directory using pushd at the start of the script and restoring is at the end of the script using popd. This way you can always assume the working directory is the same as the location of the bat file.

pushd %~dp0
ProgramInSameFolderAsBat.exe
popd


As Stephen C said, to properly support paths with spaces we can use:

start "%~dp0" "myfile.exe"

or with arguments:

start "%~dp0" "myfile.exe" -my_arguments

0

精彩评论

暂无评论...
验证码 换一张
取 消