2008-12-05 52 views
21

在Windows批处理文件中,我想查看该批处理文件的完全限定路径名是什么。查找正在运行的批处理文件的文件名

我试过%0,但这只能给我输入命令(例如只是没有路径或扩展名的文件名)。

+0

我不太明白你的要求。 %0是您键入的启动脚本的内容。脚本是否启动了另一个?它与另一个链接?请提供更多信息。 – jim 2008-12-05 11:13:57

+0

如果您只是使用%0,您将无法获得完全限定的路径名​​称。为了引用其他应用程序中的批处理文件%0是不够的。 – Martin 2008-12-08 10:25:42

回答

23
echo %~f0 

为我工作。

7

%CD%给出当前目录。

%〜DP0会给你的目录中的脚本是

IE:脚本在C:\文件夹中,我把它从C:\ otherfolder

%CD%= C:\ otherfolder

%〜DP0 = C:\文件夹

(我敢肯定,99%我已经得到了全面的正确的方式,但没有拿到窗口去检查ATM)。

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

30

为了您的信息,

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

SEE内容: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. 
+0

只是用0代替我 – waza123 2016-05-17 18:19:13

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