找出正在运行的batch file的文件名

在一个Windowsbatch file里,我想找出这个batch file的全限定path名是什么。

我尝试了%0,但这只是给了我input的命令(例如只是没有path或扩展名的文件名)。

Solutions Collecting From Web of "找出正在运行的batch file的文件名"

echo %~f0 

为我工作。

供你参考,

您将需要启用命令扩展,这是Win 2000之前不存在(我不知道NT4)

看:cmd.exe /?

 /E:ON Enable command extensions (see below) /E:OFF Disable command extensions (see below) 

在Windows上默认启用命令扩展。

我建议阅读的另一个帮助是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. 

%CD%给出当前目录。

%〜dp0会给你脚本所在的目录。

IE:脚本在c:\文件夹,我从c:\ otherfolder调用它

%CD%= C:\ otherfolder

%〜dp0 = c:\文件夹

(我99%肯定我有正确的方法,但没有窗户检查atm)。

编辑:从那里,使用你已经得到的,你应该能够得到批处理文件的名称

“%〜F0”

“%〜dpnx0”

上面的任何一个都给出了完全合格的路径。 将其用双引号括起来,以防路径中包含空格。

电话脚本:“FIRST.BAT”:

 call second.bat %0 parameter-a parameter-b 

被调用的脚本:“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