在批处理文件中执行子字符串的最佳方法是什么? [英] What is the best way to do a substring in a batch file?
问题描述
我想获取当前运行的批处理文件的名称没有文件扩展名.
I want to get the name of the currently running batch file without the file extension.
感谢这个链接,我有带有扩展名的文件名...但是在批处理文件中执行子字符串的最佳方法是什么?
Thanks to this link, I have the file name with the extension... but what is the best way to do a substring in a batch file?
或者有没有其他方法可以获取不带扩展名的文件名?
Or is there another way to get the file name w/o the extension?
在这种情况下假设 3 个字母的扩展名是安全的.
It is safe to assume 3 letter extensions in this scenario.
推荐答案
好吧,要获取批处理的文件名,最简单的方法就是使用 %~n0
.
Well, for just getting the filename of your batch the easiest way would be to just use %~n0
.
@echo %~n0
将输出当前运行的批处理文件的名称(不带扩展名)(除非在由 call
调用的子程序中执行).可以通过 help for
找到路径名的此类特殊"替换的完整列表,位于帮助的末尾:
will output the name (without the extension) of the currently running batch file (unless executed in a subroutine called by call
). The complete list of such "special" substitutions for path names can be found with help for
, at the very end of the help:
另外,替换为 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
然而,要准确回答您的问题:使用 :~start,length
符号完成子字符串:
To precisely answer your question, however: Substrings are done using the :~start,length
notation:
%var:~10,5%
将从环境变量 %var%
中的位置 10 中提取 5 个字符.
will extract 5 characters from position 10 in the environment variable %var%
.
注意:字符串的索引是从零开始的,所以第一个字符在位置 0,第二个在 1,等等.
NOTE: The index of the strings is zero based, so the first character is at position 0, the second at 1, etc.
要获取参数变量的子字符串,例如 %0
、%1
等,您必须使用 set将它们分配给普通环境变量代码>第一:
To get substrings of argument variables such as %0
, %1
, etc. you have to assign them to a normal environment variable using set
first:
:: Does not work:
@echo %1:~10,5
:: Assign argument to local variable first:
set var=%1
@echo %var:~10,5%
语法更强大:
%var:~-7%
从%var%
中提取最后 7 个字符%var:~0,-4%
将提取除最后四个字符之外的所有字符,这也将消除文件扩展名(假设句点 [.
. 后有三个字符)代码>]).
%var:~-7%
extracts the last 7 characters from%var%
%var:~0,-4%
would extract all characters except the last four which would also rid you of the file extension (assuming three characters after the period [.
]).
有关该语法的详细信息,请参阅帮助集
.
See help set
for details on that syntax.
这篇关于在批处理文件中执行子字符串的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!