%~d0 在 Windows 批处理文件中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/112055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does %~d0 mean in a Windows batch file?
提问by Chris Smith
I'm looking at a batch file which defines the following variables:
我正在查看一个定义以下变量的批处理文件:
set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0
- What do
%~d0
or%~p0
actually mean? - Is there a set of well-known values for things like current directory, drive, parameters to a script?
- Are there any other similar shortcuts I could use?
- 是什么意思
%~d0
或%~p0
实际上是什么意思? - 是否有一组众所周知的值,例如当前目录、驱动器、脚本参数?
- 我可以使用其他类似的快捷方式吗?
回答by JacquesB
The magic variables %
ncontains the arguments used to invoke the file: %0
is the path to the bat-file itself, %1
is the first argument after, %2
is the second and so on.
魔术变量%
n包含用于调用文件的参数:%0
是 bat 文件本身的路径,%1
是后面的第一个参数,%2
是第二个,依此类推。
Since the arguments are often file paths, there is some additional syntax to extract parts of the path. ~d
is drive, ~p
is the path (without drive), ~n
is the file name. They can be combined so ~dp
is drive+path.
由于参数通常是文件路径,所以有一些额外的语法来提取部分路径。~d
是驱动器,~p
是路径(不带驱动器),~n
是文件名。它们可以组合,~dp
驱动器+路径也是如此。
%~dp0
is therefore pretty useful in a bat: it is the folder in which the executing bat file resides.
%~dp0
因此在 bat 中非常有用:它是执行 bat 文件所在的文件夹。
You can also get other kinds of meta info about the file: ~t
is the timestamp, ~z
is the size.
您还可以获得有关文件的其他类型的元信息:~t
是时间戳,~z
是大小。
Look herefor a reference for all command line commands. The tilde-magic codes are described under for.
回答by efotinis
They are enhanced variable substitutions. They modify the %N variables used in batch files. Quite useful if you're into batch programming in Windows.
它们是增强的变量替换。他们修改批处理文件中使用的 %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
You can find the above by running FOR /?
.
您可以通过运行找到以上内容FOR /?
。
回答by Clewaks
Yes, There are other shortcuts that you can use which are given below. In your command, ~d0 would mean the drive letter of the 0th argument.
是的,您可以使用其他快捷方式,如下所示。在您的命令中, ~d0 表示第 0 个参数的驱动器号。
~ expands the given variable
d gets the drive letter only
0 is the argument you are referencing
As the 0th argument is the script path, it gets the drive letter of the path for you. You can use the following shortcuts too.
由于第 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
This can be also found directly in command prompt when you run CALL /? or FOR /?
当您运行 CALL /? 时,这也可以直接在命令提示符中找到。或为 /?
回答by William Keller
From Filename parsing in batch file and more idioms - Real's How-to:
从批处理文件中的文件名解析和更多习语 - Real's How-to:
The path (without drive) where the script is : ~p0
脚本所在的路径(不含驱动器):~p0
The drive where the script is : ~d0
脚本所在的驱动器:~d0
回答by Marvin Thobejane
Another tip that would help a lot is that to set the current directory to a different driveone would have to use %~d0
first, then cd %~dp0
. This will change the directory to the batch file's drive, then change to its folder.
另一个很有帮助的提示是,将当前目录设置为不同的驱动器,必须首先使用%~d0
,然后使用cd %~dp0
. 这会将目录更改为批处理文件的驱动器,然后更改为其文件夹。
For #oneLinerLovers, cd /d %~dp0
will change both the drive and directory :)
对于#oneLinerLovers,cd /d %~dp0
将同时更改驱动器和目录:)
Hope this helps someone.
希望这可以帮助某人。
回答by Armin Ronacher
%~d0
gives you the drive letter of argument 0 (the script name), %~p0
the path.
%~d0
为您提供参数 0(脚本名称)的驱动器号,%~p0
即路径。
回答by Pacerier
Some gotchas to watch out for:
需要注意的一些问题:
If you double-clickthe batch file %0
will be surrounded by quotes. For example, if you save this file as c:\test.bat
:
如果双击批处理文件%0
将被引号包围。例如,如果您将此文件另存为c:\test.bat
:
@echo %0
@pause
Double-clicking it will open a new command prompt with output:
双击它将打开一个带有输出的新命令提示符:
"C:\test.bat"
But if you first open a command prompt and call it directly from that command prompt, %0
will refer to whatever you've typed. If you type test.bat
Enter, the output of %0
will have no quotes because you typed no quotes:
但是,如果您首先打开命令提示符并直接从该命令提示符调用它,%0
则将引用您键入的任何内容。如果您输入test.bat
Enter,则输出%0
将没有引号,因为您没有输入引号:
c:\>test.bat
test.bat
If you type test
Enter, the output of %0
will have no extension too, because you typed no extension:
如果您键入test
Enter, 的输出也%0
将没有扩展名,因为您没有键入扩展名:
c:\>test
test
Same for tEsT
Enter:
同样适用于 tEsT
Enter:
c:\>tEsT
tEsT
If you type "test"
Enter, the output of %0
will have quotes (since you typed them) but no extension:
如果你输入"test"
Enter,输出%0
将有引号(因为你输入了它们)但没有扩展名:
c:\>"test"
"test"
Lastly, if you type "C:\test.bat"
, the output would be exactly as though you've double clicked it:
最后,如果您键入"C:\test.bat"
,输出将与您双击它完全一样:
c:\>"C:\test.bat"
"C:\test.bat"
Note that these are not all the possible values %0
can be because you can call the script from other folders:
请注意,这些不是所有可能的值,%0
因为您可以从其他文件夹调用脚本:
c:\some_folder>/../teST.bAt
/../teST.bAt
All the examples shown above will also affect %~0
, because the output of %~0
is simply the output of %0
minus quotes (if any).
上面显示的所有示例也会影响%~0
,因为 的输出%~0
只是%0
减引号(如果有)的输出。
回答by djangofan
This code explains the use of the ~tilda character, which was the most confusing thing to me. Once I understood this, it makes things much easier to understand:
这段代码解释了 ~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
回答by zask
It displays the current location of the file or directory that you are currently in. for example; if your batch file was in the desktop directory, then "%~dp0" would display the desktop directory. if you wanted it to display the current directory with the current file name you could type "%~dp0%~n0%~x0".
它显示您当前所在的文件或目录的当前位置。例如;如果您的批处理文件在桌面目录中,则“%~dp0”将显示桌面目录。如果您希望它以当前文件名显示当前目录,您可以键入“%~dp0%~n0%~x0”。