2008-12-05 93 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