BAT脚本中的相对路径

170

这是我在U盘上的程序文件夹:

Program\
     run.bat
     bin\
         config.ini
         Iris.exe
         library.dll
         etc.

我想使用run.bat来启动Iris.exe

我不能像使用快捷方式一样使用F:/Program/bin/Iris.exe,因为有时它不会作为驱动器F:(例如E:G:)附加。

我需要在bat文件中编写什么内容才能使其在不考虑驱动器字母的情况下正常工作?

我在BAT文件中尝试了以下内容:

"\bin\Iris.exe"

但它不起作用。

6个回答

347

在你的批处理文件中使用以下命令:

%~dp0\bin\Iris.exe

%~dp0解析为批处理脚本所在文件夹的完整路径。


10
实际上,这会解析为类似于 C:\myDir\\bin\Iris.exe 的路径(请注意双反斜杠)。尽管该路径仍然有效,但省略 bin 前面的反斜杠似乎更加“简洁”?可以使用 %~dp0bin\Iris.exe 来代替。 - mozzbozz
12
如果您可以保证 %~dp0 始终包含一个末尾反斜杠,那么这两个语句都能正常工作。否则,带有额外反斜杠的那个语句是更安全的选择。 - Ansgar Wiechers
5
好的,这有一点道理。我只在两台不同的Windows 7机器上进行了测试,其他系统(比如XP、Vista或Windows 8)可能会有所不同,但是由于微软的逻辑,我找不到任何文档来证实这一点。然而,我发现我必须将引号放在路径周围 ("%~dp0\bin\Iris.exe"),因为路径中有一个空格 :) 这样可以确保它在每台计算机上都能正常工作。 - mozzbozz
2
你可以通过 SET "scriptdir=%~dp0" 确保反斜杠存在,然后在下一行使用 IF NOT "%scriptdir:~-1%"=="\" SET "scriptdir=%scriptdir%\"。我见过路径中间双反斜杠导致软件崩溃的情况。 - LogicDaemon

52
您可以使用以下代码获取所有所需的文件属性:
FOR %%? IN (file_to_be_queried) DO (
    ECHO File Name Only       : %%~n?
    ECHO File Extension       : %%~x?
    ECHO Name in 8.3 notation : %%~sn?
    ECHO File Attributes      : %%~a?
    ECHO Located on Drive     : %%~d?
    ECHO File Size            : %%~z?
    ECHO Last-Modified Date   : %%~t?
    ECHO Parent Folder        : %%~dp?
    ECHO Fully Qualified Path : %%~f?
    ECHO FQP in 8.3 notation  : %%~sf?
    ECHO Location in the PATH : %%~dp$PATH:?
)

如何从绝对路径获取相对路径? - Sourav Kannantha B

35
我发现%CD%提供的是调用脚本的路径,而非脚本本身的路径;相反,%~dp0则提供了脚本本身的路径。

17

您应该能够使用当前目录

"%CD%"\bin\Iris.exe


1
当当前目录不是“Program”时,此操作会失败。这种情况通常发生在从资源管理器中双击“run.bat”文件时。其中,%CD%表示当前目录,%~dp0表示批处理文件本身所在的目录。 - jeb
当您从资源管理器中双击批处理文件时,这种情况不会发生,因为资源管理器使用包含目录设置来运行事物。唯一的例外是快捷方式,在那里您可以手动更改启动快捷方式的目录;但即使在那里,默认值也是快捷方式目标的包含文件夹,即等同于%~dp0。使用相对路径的失败场景是手动运行批处理文件或从另一个批处理文件(或程序)运行时。 - DimeCadmium

6

可以使用以下两种方式运行Iris.exe:
1. bin\Iris.exe(无前导斜杠 - 因为这意味着从根目录开始)
2. \Program\bin\Iris.exe(完整路径)


1
bin\Iris.exe 不起作用 :( 我不想使用 root,因为将来我可能会将此目录移动到其他位置。 如果我向操作系统请求当前的绝对路径呢?然后在 bin 中使用该路径启动 exe,这样怎么样? - user2083037
我假设当前驱动器是USB存储器的驱动器,当前文件夹将是\Program - 不是这种情况吗?您可以在尝试运行.exe之前的行中使用简单的cd命令来显示。 - AjV Jsy
仅仅使用相对路径并不一定有效。该路径将相对于当前工作目录,而该目录可能与 run.bat 的父目录不同。 - Ansgar Wiechers

0
%~dp0 will return the drive and the path
so try two things
%~dp0 --> will give ..F:\Program\run.bat
now inside the .bat file if we want to access exe then use below
pushd "%~dp0" 
..\bin\Iris.exe 

Explanation
-->PUSHD command to get the directory of the batch script file in the Windows operating system.
-->while %CD% can be used on command line.
for %CD%, the current directory means the directory when executing the command line or the batch file. So if you put the batch file in c:\dir\test.bat --> @echo %CD%, if you are now in C:\dir and execute test.bat, it will output c:\dir;

-->%~dp0 can only be used in bat file 
For %~dp0, the current directory is the directory where the bat file resides. In above example - the output is the same: c:\dir\. Note there is a back slash at the end.
Hope this helps!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接