什么是做在批处理文件中的子字符串的最佳方式? [英] What is the best way to do a substring in a batch file?
问题描述
我想获得当前正在运行的批处理文件中的没有文件扩展名。
由于<一个href=\"http://stackoverflow.com/questions/343518/finding-out-the-file-name-of-the-running-batch-file\">this链接,我有文件名的与的延伸......但什么是做一个字符串在批处理文件的最好方法?
或者有另一种方式来获得文件名W / O扩展?
这是安全的假设在这种情况下3个字母的扩展。
好了,只是让你批次的文件名,最简单的方法是只使用%〜N0
。
@echo%〜N0
当前正在运行的批处理文件将输出名称(不带扩展名)(除非在由呼叫调用的子程序执行
)。为路径名这样的特殊换人的完整列表可与帮助下找到了
,在帮助最末尾:
在另外,对于取代
变量引用已得到增强。
您现在可以使用下列可选
语法:%〜I - 将%I扩充删除任何引号()
%〜FI - 将%I扩充到一个完全合格的路径名
%〜dI - 仅将%I扩充到一个驱动器号
%〜pI - 仅将%I扩充到一个路径
%〜镍 - 将%I扩充到一个文件名只
%〜曦 - %I扩充到一个文件扩展名
%〜的sI - 扩充的路径只含有短名
%〜aI - 将%I扩充到文件的文件属性
%〜TI - %I扩充到文件的日期/时间
%〜滋 - %I扩充到文件的大小
%〜$ PATH:I - 查找列在路径中列出的目录
环境变量,将%I扩充到
第一个发现了完全合格的名称。
如果环境变量名是不
定义或该文件不被发现
搜索,那么这个修改扩展到
空字符串
的改性剂可被组合以得到
多重结果:%〜DPI - 将%I扩充到一个驱动器号和路径只
%〜NXI - 将%I扩充到一个文件名和扩展名
%〜FSI - %I扩充到,只有短名称的全路径名
块引用>要precisely回答你的问题,但是:子串使用做了
:〜启动,长度
注释:%VAR:10,5〜%
将在环境变量中提取10位5个字符
%VAR%
。注意:字符串的索引是从零开始的,所以第一个字符的位置是0,第二个为1,等等。
要获取参数变量的子串,如
0%
,%1
等,你必须为它们分配使用集
一个正常的环境变量第一:::不工作:
@echo%1:10,5〜::第一个参数指定局部变量:
组VAR =%1
@echo%VAR:10,5〜%语法更是如虎添翼:
- 提取后7个字符
%VAR:〜-7%
从%VAR%
%VAR:〜0,-4%
将提取除了过去四年中的所有字符也将摆脱你的文件扩展名(期后假设三个字符[。
])。请参阅
帮助集
有关该语法的详细信息。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?
It is safe to assume 3 letter extensions in this scenario.
解决方案Well, for just getting the filename of your batch the easiest way would be to just use
%~n0
.@echo %~n0
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 withhelp for
, at the very end of the help: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
To precisely answer your question, however: Substrings are done using the
:~start,length
notation:%var:~10,5%
will extract 5 characters from position 10 in the environment variable
%var%
.NOTE: The index of the strings is zero based, so the first character is at position 0, the second at 1, etc.
To get substrings of argument variables such as
%0
,%1
, etc. you have to assign them to a normal environment variable usingset
first::: Does not work: @echo %1:~10,5 :: Assign argument to local variable first: set var=%1 @echo %var:~10,5%
The syntax is even more powerful:
%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屋!