我想获取当前正在运行的批处理文件的名称, 不带 文件扩展名。
感谢这个链接,我有了 带有 扩展名的文件名......但是在批处理文件中执行子字符串的最佳方法是什么?
还是有另一种方法来获取不带扩展名的文件名?
在这种情况下,假设 3 个字母扩展是安全的。
好吧,为了获取批处理的文件名,最简单的方法就是使用%~n0.
%~n0
@echo %~n0
将输出当前运行的批处理文件的名称(不带扩展名)(除非在由 调用的子例程中执行call)。此类“特殊”路径名替换的完整列表可以help for在帮助的最后找到:
call
help for
此外,增强了 FOR 变量引用的替换。您现在可以使用以下可选语法: %~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 可以组合修饰符以获得复合结果: %~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
此外,增强了 FOR 变量引用的替换。您现在可以使用以下可选语法:
%~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
可以组合修饰符以获得复合结果:
%~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
但是,要准确回答您的问题:子字符串使用以下:~start,length符号完成:
:~start,length
%var:~10,5%
将从环境变量中的位置 10 中提取 5 个字符%var%。
%var%
注意: 字符串的索引是从零开始的,所以第一个字符在位置 0,第二个在 1,依此类推。
要获取参数变量的子字符串,例如%0,%1等,您必须首先使用以下方法将它们分配给普通环境变量set:
%0
%1
set
:: Does not work: @echo %1:~10,5 :: Assign argument to local variable first: set var=%1 @echo %var:~10,5%
语法更强大:
%var:~-7%
%var:~0,-4%
.
有关help set该语法的详细信息,请参阅。
help set