一个 Windows 批处理文件能否确定自己的文件名?
例如,如果我运行批处理文件 C:\Temp\myScript.bat,是否有一条命令可以在 myScript.bat 文件中确定字符串 "myScript.bat"?
一个 Windows 批处理文件能否确定自己的文件名?
例如,如果我运行批处理文件 C:\Temp\myScript.bat,是否有一条命令可以在 myScript.bat 文件中确定字符串 "myScript.bat"?
可以。
使用特殊变量%0
获取当前文件的路径。
写入%~n0
以仅获取不带扩展名的文件名。
写入%~n0%~x0
以获取文件名和扩展名。
也可以写%~nx0
以获取文件名和扩展名。
%cd%
将返回调用批处理文件的进程的当前目录。因此,如果您在 D:\ dir1
提示符处,并输入 Y:\ foo\bar.bat
,如果 bar.bat
具有 @echo % cd%
,则将输出 D:\ dir1
。 - Preacher您可以获取文件名,但是也可以获取完整路径,具体取决于您在“%~”和“0”之间放置的内容。从以下选项中选择:
d -- drive
p -- path
n -- file name
x -- extension
f -- full path
比如,从c:\tmp\foo.bat内部,%~nx0
会给你"foo.bat",而%~dpnx0
会给你"c:\tmp\foo.bat"。请注意,这些片段总是按照规范顺序组装的,因此如果你聪明地尝试%~xnpd0
,你仍然会得到"c:\tmp\foo.bat"。
f
是文件名,但在你的例子中你用了 n
作为文件名。经过一些测试,似乎 f
是完整路径,而 n
是文件名。 - Drew Chapin%~xnpd0
...我爱你。 - Mark Deven使用以下基于SLaks的回答的脚本,我确定正确答案是:
echo The name of this file is: %~n0%~x0
echo The name of this file is: %~nx0
这是我的测试脚本:
@echo off
echo %0
echo %~0
echo %n0
echo %x0
echo %~n0
echo %dp0
echo %~dp0
pause
我觉得有趣的是,我们知道波浪线字符通常用于去掉变量的引号/修剪引号,但%nx0却不起作用。
0
是一种特殊情况,其中0
表示命令行上给出的此文件。%0
或%~0
使用中给出不同的输出:
myfile
myfile.bat
mydir\myfile.bat
c:\mydir\myfile.bat
"c:\mydir\myfile.bat"
如果您从该文件所在目录的正确相对位置调用它,则以上所有调用都是合法的。 %~0
会从最后一个示例中去掉引号,而%0
则不会。%0
和%~0
很可能不是您想要使用的内容。@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
@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
%~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
@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.
set b=%~0
echo %~n0%b:~8,4%
我了解到“:〜8,4%”的意思是从变量的第9个字符开始显示接下来的4个字符。范围是从0到变量字符串的结尾。所以%Path%会非常长!
fIlEnAmE.bat
012345678901
然而,这并不像Jool的解决方案(%~x0)那样可靠。
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
@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
%0
就是你要找的。请参阅此问题获取更多信息。 - Jonathon Reinhart