如何执行与 Windows 批处理文件在同一目录中的程序?

我在同一个文件夹中有一个 .bat和一个 .exe文件。 我不能从 .bat调用 .exe文件,除非我将完整的绝对路径放到它上面。 有没有办法不指定路径?

125900 次浏览

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