查找正在运行的批处理文件的文件名

28

我希望在Windows批处理文件中找出该批处理文件的完全限定路径名。

我尝试过使用%0,但这只给出了输入的命令(例如,只有文件名而没有路径或扩展名)。


我不太明白你的问题。%0 是你输入以启动脚本的内容。这个脚本是启动另一个脚本吗?还是与另一个脚本相关联?请提供更多信息。 - jim
如果你只使用%0,你将无法获得完全限定的路径名。为了在其他应用程序中引用你的批处理文件,%0是不够的。 - Martin
5个回答

35

提供资讯,

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

请参阅:cmd.exe /?

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

在Windows系统中,命令扩展默认是启用的。

我建议您阅读另一个帮助文档,即FOR命令。它包含了这些标志的完整含义。

请参阅:从cmd运行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.

just replace I with 0 - jmp

28
echo %~f0

适用于我的情况。

cmd中查看for /?并阅读有关变量替换的内容。


8

%CD% 提供当前目录。

%~dp0 将提供脚本所在的目录。

例如:脚本位于c:\folder,我从c:\otherfolder调用它

%CD% = C:\otherfolder

%~dp0 = c:\folder

(我99%确定我已经将它们正确地交换了,但目前没有Windows可供检查)。

编辑:从那里开始,使用你已经拥有的这个,你应该能够获取批处理文件名。


1
%~f0

%~dpnx0

以上两种方法都可以得到完全限定路径。如果路径包含空格,请用双引号括起来。


0

调用脚本 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

我希望我的格式改进大致正确。 - zx485

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