我想获取当前运行的批处理文件的名称,但不带文件扩展名。
多亏了这个链接,我有了扩展名为…但是在批处理文件中处理子字符串的最佳方法是什么?
或者是否有另一种方法来获得没有扩展名的文件名?
在这个场景中,可以安全地假设有3个字母扩展名。
我想获取当前运行的批处理文件的名称,但不带文件扩展名。
多亏了这个链接,我有了扩展名为…但是在批处理文件中处理子字符串的最佳方法是什么?
或者是否有另一种方法来获得没有扩展名的文件名?
在这个场景中,可以安全地假设有3个字母扩展名。
当前回答
对于获取批处理的文件名,最简单的方法是使用%~n0。
@echo %~n0
将输出当前运行的批处理文件的名称(不带扩展名)(除非在由call调用的子例程中执行)。路径名的这种“特殊”替换的完整列表可以在帮助中找到,在帮助的最后:
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
然而,为了精确地回答你的问题:子字符串使用:~开始,长度符号:
%var:~10,5%
将从环境变量%var%的第10位提取5个字符。
注意:字符串的索引是以0为基础的,因此第一个字符位于位置0,第二个字符位于位置1,等等。
要获得参数变量的子字符串,如%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%中提取最后7个字符 %var:~0,-4%将提取除最后四个字符以外的所有字符,这也将消除文件扩展名(假设句点[.]后有三个字符)。
有关该语法的详细信息,请参阅帮助设置。
其他回答
作为Joey回答的额外信息,这在set /?也不是for /?
%~0展开为自己批处理的名称,与输入时完全相同。 所以如果你开始你的批处理,它会被扩展为
%~0 - mYbAtCh
%~n0 - mybatch
%~nx0 - mybatch.bat
但是有一个例外,在子例程中展开可能会失败
echo main- %~0
call :myFunction
exit /b
:myFunction
echo func - %~0
echo func - %~n0
exit /b
结果是
main - myBatch
Func - :myFunction
func - mybatch
在函数中%~0始终展开为函数的名称,而不是批处理文件的名称。 但是如果你使用至少一个修饰符,它会再次显示文件名!
上面解释得很好!
对于所有那些可能像我一样痛苦的人来说,在本地化的Windows中工作(我的是XP in Slovak),你可以尝试用%替换!
所以:
SET TEXT=Hello World
SET SUBSTRING=!TEXT:~3,5!
ECHO !SUBSTRING!
对于获取批处理的文件名,最简单的方法是使用%~n0。
@echo %~n0
将输出当前运行的批处理文件的名称(不带扩展名)(除非在由call调用的子例程中执行)。路径名的这种“特殊”替换的完整列表可以在帮助中找到,在帮助的最后:
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
然而,为了精确地回答你的问题:子字符串使用:~开始,长度符号:
%var:~10,5%
将从环境变量%var%的第10位提取5个字符。
注意:字符串的索引是以0为基础的,因此第一个字符位于位置0,第二个字符位于位置1,等等。
要获得参数变量的子字符串,如%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%中提取最后7个字符 %var:~0,-4%将提取除最后四个字符以外的所有字符,这也将消除文件扩展名(假设句点[.]后有三个字符)。
有关该语法的详细信息,请参阅帮助设置。