Windows 批处理文件可以确定自己的文件名吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8797983/
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 09:05:57  来源:igfitidea点击:

Can a Windows batch file determine its own file name?

windowsbatch-filecmd

提问by djangofan

Can a Windows batch file determine its own file name?

Windows 批处理文件可以确定自己的文件名吗?

For example, if I run the batch file C:\Temp\myScript.bat, is there a command within myScript.bat that can determine the string "myScript.bat"?

例如,如果我运行批处理文件 C:\Temp\myScript.bat,myScript.bat 中是否有可以确定字符串“myScript.bat”的命令?

回答by SLaks

Yes.

是的。

Use the special %0variable to get the path to the current file.

使用特殊%0变量获取当前文件的路径。

Write %~n0to get just the filename without the extension.

写入%~n0只获取没有扩展名的文件名。

Write %~n0%~x0to get the filename and extension.

写入%~n0%~x0以获取文件名和扩展名。

Also possible to write %~nx0to get the filename and extension.

也可以写入%~nx0以获取文件名和扩展名。

回答by JustJeff

You can get the file name, but you can also get the full path, depending what you place between the '%~' and the '0'. Take your pick from

您可以获得文件名,但也可以获得完整路径,具体取决于您在“%~”和“0”之间放置的内容。从中挑选

d -- drive
p -- path
n -- file name
x -- extension
f -- full path

E.g., from inside c:\tmp\foo.bat, %~nx0gives you "foo.bat", whilst %~dpnx0gives "c:\tmp\foo.bat". Note the pieces are always assembled in canonical order, so if you get cute and try %~xnpd0, you stillget "c:\tmp\foo.bat"

例如,从 c:\tmp\foo.bat 内部,%~nx0给你“foo.bat”,同时%~dpnx0给你“c:\tmp\foo.bat”。注意这些部分总是按照规范顺序组装的,所以如果你很可爱并尝试%~xnpd0,你仍然会得到“c:\tmp\foo.bat”

回答by djangofan

Using the following script, based on SLaks answer, I determined that the correct answer is:

使用以下脚本,根据 SLaks 答案,我确定正确答案是:

echo The name of this file is: %~n0%~x0
echo The name of this file is: %~nx0

And here is my test script:

这是我的测试脚本:

@echo off
echo %0
echo %~0
echo %n0
echo %x0
echo %~n0
echo %dp0
echo %~dp0
pause

What I find interesting is that %nx0won't work, given that we know the '~' char usually is used to strip/trim quotes off of a variable.

我觉得有趣的是%nx0不起作用,因为我们知道 '~' 字符通常用于从变量中去除/修剪引号。

回答by Jool

Bear in mind that 0is a special case of parameter numbers inside a batch file, where 0means this file as given on the command line.

请记住,这0是批处理文件中参数编号的一种特殊情况,其中0表示该文件在命令行中给出

So if the file is myfile.bat, you could call it in several ways as follows, each of which would give you a differentoutput from the %0or %~0usage:

因此,如果文件是 myfile.bat,您可以通过以下几种方式调用它,每种方式都会为您提供与or用法不同的输出:%0%~0

myfile

myfile

myfile.bat

myfile.bat

mydir\myfile.bat

mydir\myfile.bat

c:\mydir\myfile.bat

c:\mydir\myfile.bat

"c:\mydir\myfile.bat"

"c:\mydir\myfile.bat"

All of the above are legal calls if you call it from the correct relative place to the directory in which it exists. %~0strips the quotes from the last example, whereas %0does not.

如果您从正确的相对位置调用它,那么上述所有调用都是合法调用。 %~0去掉最后一个例子中的引号,而%0没有。

Because these all give different results, %0and %~0are very unlikely to be what you actually want to use.

因为这些都会给出不同的结果,%0并且%~0不太可能是您真正想要使用的。

Here's a batch file to illustrate:

这是一个批处理文件来说明:

@echo Full path and filename: %~f0
@echo Drive: %~d0
@echo Path: %~p0
@echo Drive and path: %~dp0
@echo Filename without extension: %~n0
@echo Filename with    extension: %~nx0
@echo Extension: %~x0
@echo Filename as given on command line: %0
@echo Filename as given on command line minus quotes: %~0
@REM Build from parts
@SETLOCAL
@SET drv=%~d0
@SET pth=%~p0
@SET fpath=%~dp0
@SET fname=%~n0
@SET ext=%~x0
@echo Simply Constructed name: %fpath%%fname%%ext%
@echo Fully  Constructed name: %drv%%pth%%fname%%ext%
@ENDLOCAL
pause

回答by Cary

Try to run below example in order to feel how the magical variables work.

尝试运行下面的示例以感受神奇变量的工作原理。

@echo off

SETLOCAL EnableDelayedExpansion

echo Full path and filename: %~f0
echo Drive: %~d0
echo Path: %~p0
echo Drive and path: %~dp0
echo Filename without extension: %~n0
echo Filename with    extension: %~nx0
echo Extension: %~x0

echo date time : %~t0
echo file size: %~z0

ENDLOCAL

The related rules are following.

相关规则如下。

%~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

回答by Fred Tusing

Below is my initial code:

下面是我的初始代码:

@echo off
Set z=%%
echo.
echo %z%0.......%0
echo %z%~0......%~0
echo %z%n0......%n0
echo %z%x0......%x0
echo %z%~n0.....%~n0
echo %z%dp0.....%dp0
echo %z%~dp0....%~dp0
echo.

I noticed that file name given by %~0 and %0 is the way it was entered in the command-shell and not how that file is actually named. So if you want the literal case used for the file name you should use %~n0. However, this will leave out the file extension. But if you know the file name you could add the following code.

我注意到 %~0 和 %0 给出的文件名是它在命令外壳中输入的方式,而不是该文件的实际命名方式。因此,如果您想要用于文件名的字面大小写,则应使用 %~n0。但是,这将忽略文件扩展名。但是如果您知道文件名,则可以添加以下代码。

set b=%~0
echo %~n0%b:~8,4%

I have learned that ":~8,4%" means start at the 9th character of the variable and then show show the next 4 characters. The range is 0 to the end of the variable string. So %Path% would be very long!

我了解到 ":~8,4%" 表示从变量的第 9 个字符开始,然后显示接下来的 4 个字符。范围是 0 到变量字符串的末尾。所以 %Path% 会很长!

fIlEnAmE.bat
012345678901

However, this is not as sound as Jool's solution (%~x0) above.

但是,这并不像上面 Jool 的解决方案 (%~x0) 那样健全。

My Evidence:

我的证据:

C:\bin>filename.bat

%0.......filename.bat
%~0......filename.bat
. . .

C:\bin>fIlEnAmE.bat

%0.......fIlEnAmE.bat
%~0......fIlEnAmE.bat
%n0......n0
%x0......x0
%~n0.....FileName
%dp0.....dp0
%~dp0....C:\bin\

%~n0%b:~8,4%...FileName.bat

Press any key to continue . . .

C:\bin>dir
 Volume in drive C has no label.
 Volume Serial Number is CE18-5BD0

 Directory of C:\bin
. . .
05/02/2018  11:22 PM               208 FileName.bat


Here is the final code

这是最终的代码

@echo off
Set z=%%
set b=%~0
echo.
echo %z%0.......%0
echo %z%~0......%~0
echo %z%n0......%n0
echo %z%x0......%x0
echo %z%~n0.....%~n0
echo %z%dp0.....%dp0
echo %z%~dp0....%~dp0
echo.
echo A complex solution:
echo ===================================
echo %z%~n0%z%b:~8,4%z%...%~n0%b:~8,4%
echo ===================================
echo.
echo The preferred solution:
echo ===================================
echo %z%~n0%z%~x0.......%~n0%~x0
echo ===================================
pause