2008-09-21 257 views
260

我期待在一个批处理文件,它定义了以下变量:%〜d0在Windows批处理文件中的含义是什么?

set _SCRIPT_DRIVE=%~d0 
set _SCRIPT_PATH=%~p0 
  • 什么%~d0%~p0实际上意味着什么呢?
  • 是否有一套众所周知的值,像当前目录,驱动器,脚本参数?
  • 有没有其他类似的捷径可以使用?

回答

413

神奇的变量%ň包含用于调用该文件的参数:%0是路径蝙蝠文件本身,%1是第一个参数后,%2是第二等。

由于参数通常是文件路径,因此还有一些额外的语法来提取部分路径。 ~d是驱动器,~p是路径(无驱动器),~n是文件名。他们可以结合,所以~dp是驱动器+路径。

%~dp0因此在蝙蝠中非常有用:它是正在执行的bat文件所在的文件夹。

您还可以获取有关该文件的其他种类的元信息:~t是时间戳,~z是大小。

查看here以获取所有命令行命令的参考。波浪魔法代码在for下描述。

+1

它也在“调用”命令中描述,顺便说一句,它用于批处理文件。 :) – user314104 2011-12-06 01:37:39

+1

另请参阅http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true – 2012-12-20 20:03:39

+5

注意自己:`echo%〜dp0`会不在命令行工作,只能在批处理文件中使用。咄! – northben 2015-03-27 18:40:32

7

here

路径(无硬盘),其中脚本是:〜P0

驱动器,其中脚本是:〜D0

5

%~d0给你的驱动器盘符参数0(脚本名称),%~p0的路径。

117

它们是增强型变量替换。它们修改批处理文件中使用的%N个变量。如果您在Windows中进行批处理编程,这非常有用。

%~I   - expands %I removing any surrounding quotes ("") 
%~fI  - expands %I to a fully qualified path name 
%~dI  - expands %I to a drive letter only 
%~pI  - expands %I to a path only 
%~nI  - expands %I to a file name only 
%~xI  - expands %I to a file extension only 
%~sI  - expanded path contains short names only 
%~aI  - expands %I to file attributes of file 
%~tI  - expands %I to date/time of file 
%~zI  - expands %I to size of file 
%~$PATH:I - searches the directories listed in the PATH 
       environment variable and expands %I to the 
       fully qualified name of the first one found. 
       If the environment variable name is not 
       defined or the file is not found by the 
       search, then this modifier expands to the 
       empty string 

您可以通过运行FOR /?找到上述内容。

34

是的,还有其他的捷径可供您使用,下面给出。 在您的命令中,〜d0将表示第0个参数的驱动器号。

~ expands the given variable 
d gets the drive letter only 
0 is the argument you are referencing 

由于第0个参数是脚本路径,它将为您获取路径的驱动器号。您也可以使用以下快捷方式。

%~1   - expands %1 removing any surrounding quotes (") 
%~f1  - expands %1 to a fully qualified path name 
%~d1  - expands %1 to a drive letter only 
%~p1  - expands %1 to a path only 
%~n1  - expands %1 to a file name only 
%~x1  - expands %1 to a file extension only 
%~s1  - expanded path contains short names only 
%~a1  - expands %1 to file attributes 
%~t1  - expands %1 to date/time of file 
%~z1  - expands %1 to size of file 
%~$PATH:1 - searches the directories listed in the PATH 
       environment variable and expands %1 to the fully 
       qualified name of the first one found. If the 
       environment variable name is not defined or the 
       file is not found by the search, then this 
       modifier expands to the empty string  

%~dp1  - expands %1 to a drive letter and path only 
%~nx1  - expands %1 to a file name and extension only 
%~dp$PATH:1 - searches the directories listed in the PATH 
       environment variable for %1 and expands to the 
       drive letter and path of the first one found. 
%~ftza1  - expands %1 to a DIR like output line 

当您运行CALL /?时,也可以在命令提示符中直接找到此项。或FOR /?

2

该代码解释了〜tilda角色的使用,这是我最困惑的事情。有一次,我理解这一点,它使事情更容易理解:

@ECHO off 
SET "PATH=%~dp0;%PATH%" 
ECHO %PATH% 
ECHO. 
CALL :testargs "these are days" "when the brave endure" 
GOTO :pauseit 
:testargs 
SET ARGS=%~1;%~2;%1;%2 
ECHO %ARGS% 
ECHO. 
exit /B 0 
:pauseit 
pause 
6

另一个秘诀,将有很大的帮助是当前目录设置为不同的驱动一个将不得不使用%~d0第一,然后cd %~dp0。这会将目录更改为批处理文件的驱动器,然后更改为其文件夹。

对于#oneLinerLovers,cd /d %~dp0将同时更改驱动器和目录:)

希望这可以帮助别人。

1

的一些陷阱需要提防:

如果双击批处理文件%0将用引号引起来。例如,如果您将此文件保存为c:\test.bat

@echo %0 
@pause 

双击它会打开一个新的命令提示符与输出:

"C:\test.bat" 

但是,如果你第一次打开一个命令提示符并直接调用它从该命令提示符下,%0将参考您已输入的任何内容输入。如果您键入test.bat输入%0输出不会有任何的报价,因为您键入不带引号:

c:\>test.bat 
test.bat 

如果键入test输入%0输出将没有扩展名也是如此,因为您键入没有扩展名:

c:\>test 
test 

同为tEsT输入

c:\>tEsT 
tEsT 

如果键入"test"输入%0输出将有引号(因为你键入它们),但没有扩展名:

c:\>"test" 
"test" 

最后,如果你键入"C:\test.bat",输出会完全就好像你已经双击它:

c:\>"C:\test.bat" 
"C:\test.bat" 

请注意,这些不是所有可能的值%0可能是因为你可以从其他文件夹调用脚本:

c:\some_folder>/../teST.bAt 
/../teST.bAt 

所有上面所示的例子也会影响%~0,因为%~0输出是一个简单的%0减去引号(如果有的话)的输出。

0

它显示您当前所在文件或目录的当前位置。如果您的批处理文件位于桌面目录中,则“%〜dp0”将显示桌面目录。如果你想用当前文件名显示当前目录,你可以输入“%〜dp0%〜n0%〜x0”。

相关问题