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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/343518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:49:13  来源:igfitidea点击:

Finding out the file name of the running batch file

windowsbatch-file

提问by Martin

Inside a windows batch file I'd like to figure out what the fully qualified path name of this batch file is.

在 Windows 批处理文件中,我想弄清楚这个批处理文件的完全限定路径名是什么。

I have tried %0but this does only gave me the typed command (e.g. just the file name without path or extension).

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

回答by Dennis C

For your information,

供您参考,

You will need to enable command extends, which is not exists before Win 2000 (I don't know NT4)

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

SEE: cmd.exe /?

参见:cmd.exe /?

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

command extensions is enabled by default on windows.

Windows 上默认启用命令扩展。

Another help I suggest to read is the FOR command. It contains complete meaning for those flag.

我建议阅读的另一个帮助是 FOR 命令。它包含这些标志的完整含义。

SEE: for /?from cmd

见:for /?cmd

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.

回答by PhiLho

echo %~f0

works for me.

对我来说有效。

see for /?from cmdand read about variable substitution.

看到for /?cmd并阅读有关变量替换。

回答by benlumley

%CD%gives the current directory.

%CD%给出当前目录。

%~dp0will give you the directory the script is in.

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

IE: script in c:\folder, I call it from c:\otherfolder

IE: script in c:\folder,我从c:\otherfolder

%CD%= C:\otherfolder

%CD%= C:\otherfolder

%~dp0= c:\folder

%~dp0= c:\folder

(I'm 99% sure I've got those the right way round, but not got windows to check on atm).

(我 99% 确定我已经找到了正确的方法,但没有窗户可以检查 atm)。

edit: and from there, using the one you've already got, you should be able to get the batch file name

编辑:从那里,使用您已经拥有的,您应该能够获得批处理文件名

回答by Joe

%~f0
%~dpnx0

Either of the above gives the fully-qualified path. Enclose it in double quotes in case the path contains spaces.

以上任何一个都给出了完全限定的路径。如果路径包含空格,请将其括在双引号中。

回答by Robert Cody

Calling script FIRST.BAT:

调用脚本FIRST.BAT

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

Called script SECOND.BAT:

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