在Windows批处理文件中,我想知道这个批处理文件的完全限定路径名是什么。
我尝试了%0,但这只给了我输入的命令(例如只是没有路径或扩展名的文件名)。
答案 0 :(得分:33)
供您参考,
你需要启用命令extends,这在Win 2000之前是不存在的(我不知道NT4)
参见:cmd.exe /?
/E:ON Enable command extensions (see below)
/E:OFF Disable command extensions (see below)
默认情况下,在Windows上启用命令扩展。
我建议阅读的另一个帮助是FOR命令。 它包含那些标志的完整含义。
SEE:for /?
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~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
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
答案 1 :(得分:26)
echo %~f0
适合我。
答案 2 :(得分:8)
%CD%提供当前目录。
%~dp0将为您提供脚本所在的目录。
IE:c:\文件夹中的脚本,我从c:\ otherfolder
中调用它%CD%= C:\ otherfolder
%~dp0 = c:\ folder
(我99%肯定我有正确的方法,但没有窗户检查atm)。
编辑:从那里,使用你已经拥有的那个,你应该能够获得批处理文件名
答案 3 :(得分:1)
“%〜F0”
“%〜dpnx0”
上述任何一个都给出了完全合格的路径。如果路径包含空格,请用双引号括起来。
答案 4 :(得分:0)
打电话给:“FIRST.BAT”:
call second.bat %0 parameter-a parameter-b
CALLED SCRIPT:“SECOND.BAT”:
echo The name of this called script should be "SECOND", proof: %~n0
echo The 1st parameter passed should be "FIRST", proof: %1 shift
echo The name of the calling script should be "FIRST", proof: %~n0
echo The 1st parameter should be "parameter-a", proof: %1