我想要将一个环境变量字符串中的星号去掉,但是似乎无法实现。
我正在创建一个基于搜索字符串的m3u文件,例如,如果我想要创建一个包含所有包含单词“love”的歌曲的m3u文件,我会输入:
m3u *Love*
而 m3u.bat 将创建文件:
xLovex.m3u
但是常规的替换字符方法对星号不起作用。(虽然我在问号上没有这个问题。)
set nam=%nam:*=x%.m3u
改为创建文件名
x.m3u
我想要将一个环境变量字符串中的星号去掉,但是似乎无法实现。
我正在创建一个基于搜索字符串的m3u文件,例如,如果我想要创建一个包含所有包含单词“love”的歌曲的m3u文件,我会输入:
m3u *Love*
xLovex.m3u
但是常规的替换字符方法对星号不起作用。(虽然我在问号上没有这个问题。)
set nam=%nam:*=x%.m3u
改为创建文件名
x.m3u
简单的答案是否定的。
你遇到的问题源于使用SET搜索和替换方法时,星号*是一个特殊字符。它以一种有限但仍然有用的方式匹配多个字符。你可以在这里了解更多信息。
困难的答案是是!
我将为您提供两种解决方案。一种是不完整但优雅的解决方案,另一种是完整但不优雅的解决方案。
这两种方法都将搜索*并用x替换它。
这两种方法都将搜索并修改以下字符串:
*love*
两个版本都需要延迟环境变量扩展,但原因不同。一个是因为我在FOR语句中操作。另一个是因为您不能将%对放在另一个%对内,因为命令处理器将第二个%与它遇到的第一个%匹配,但我们需要在另一个变量表达式中使用变量。(您会看到的。)::主要编辑::
我以为我知道各种环境变量的最大大小,但dbenham让我明白了一些,他向我展示了一个kick-in-the-behind length function,同时完全改变了我对这两个解决方案的看法。
除了Windows 95/98/ME限制256个字符的最大环境变量大小外。似乎所有使用CMD.EXE的Windows版本都有8192个字符的限制,远低于文档所建议的。
setlocal ENABLEDELAYEDEXPANSION
set nam=*love*
rem calling strLen
call :strLen nam len
for /l %%x in (0,1,%len%) do if not "!nam:~%%x,1!"=="" if "!nam:~%%x,1!"=="*" (
set /a plusone=%%x+1
for /l %%y in (!plusone!, 1, !plusone!) do (
set nam=!nam:~0,%%x!x!nam:~%%y!
)
)
echo %nam%
ENDLOCAL
我认为这是一个快速而优雅的解决方案。通过将strLen.bat的内容添加到程序中,可以加快速度,但我不想让作者产生混淆。
如果出于某种原因,您不希望使用strLen,则下一个最快的方法可能是使用GOTO循环。
解决方案2:(Goto解决方案)
setlocal ENABLEDELAYEDEXPANSION
set nam=*love*
set num=0
:loop
set /a plusone=%num%+1
if "!nam:~%num%,1!"=="*" set nam=!nam:~0,%num%!x!nam:~%plusone%!
set /a num=%num%+1
if not "!nam:~%num%,1!"=="" goto :loop
echo %nam%
EndLocal
特别感谢dbenham指出了strLen函数。它的运行速度比任何基于批处理的函数都要快得多!
for /f "tokens=1,* delims=*" %%a in ("a*b") do (set string=%%a%%b)
>>> string=ab
要删除更多,我们可以使用更多的标记...
for /f "tokens=1-3,* delims=*" %%a in ("a*b*c*d") do (set string=%%a%%b%%c%%d)
>>> string=abcd
...或者我们可以将第一行放入一个for /l
循环中:
setlocal enableDelayedExpansion
set string=a*b*c*d
for /l %%a in (1, 1, 3) do (
for /f "tokens=1,* delims=*" %%b in ("!string!") do (set string=%%b%%c)
)
>>> string=abcd
另外需要注意的是,您可以在delims
中定义多个字符,它们将同时被移除:
for /f "tokens=1,* delims=+-*/" %%a in ("a*-/+b") do (set string=%%a%%b)
>>> string=ab
for /F
循环 来在每个出现特定字符(或一系列特定字符)的位置分割字符串。实际功能被封装到一个子例程中,以便轻松重用,因此以下脚本的主要部分只包含一些测试代码:@echo off
setlocal EnableExtensions DisableDelayedExpansion
::This is the main routine of the script holding code for test and demonstration:
rem // Definition of some sample text to test (note that `%%` becomes one literal `%`):
set "DATA=some text,"^&"&;0'@%%~#`$:wild**card*?.re<dir>=|+([{parens}])-^/equal==to=!_"
echo/
call :REPL_CHAR TEXT DATA "*" "?"
setlocal EnableDelayedExpansion
echo(In: !DATA!
echo(Out:!TEXT!
echo/
echo(In: !TEXT!
call :REPL_CHAR TEXT TEXT "=" "/"
echo(Out:!TEXT!
endlocal
endlocal
exit /B
:REPL_CHAR
::This function replaces in a string every occurrence of a sequence of a certain character
::by another character or a string. It even correctly handles the characters `*` and `=`.
:: USAGE:
:: call :REPL_CHAR ref_output_string ref_input_string val_search_char val_replace_char
:: PARAMETERS:
:: ref_output_string reference to (name of) variable to receive the resulting string;
:: ref_input_string reference to variable that holds the original string; if empty
:: (`""`), the variable referenced by `ref_output_string` is used;
:: val_search_char single character that is to be replaced;
:: val_replace_char character or string to replace every sequence of `val_search_char`
:: with; this may even be empty;
rem // Localise environment and detect whether delayed expansion is enabled (needed later):
setlocal & set "$NDX=!"
setlocal DisableDelayedExpansion
rem // Fetch arguments and verify them:
set "#RET=%~1" & if not defined #RET endlocal & endlocal & exit /B 2
set "#STR=%~2" & if not defined #STR set "#STR=%#RET%"
set "CHR=%~3"
if not defined CHR endlocal & endlocal & exit /B 1
set "RPL=%~4"
setlocal EnableDelayedExpansion
rem // Initialise several auxiliary variables:
set "TST=!%#STR%!" & set "CHR=!CHR:~,1!" & set "INS="
if "!CHR!"=="_" (set "BUF=#" & set "WRK=!TST!#") else (set "BUF=_" & set "WRK=!TST!_")
:REPL_CHAR_LOOP
rem // Check whether the end of the string has been reached:
if not defined TST set "BUF=!BUF:~1,-1!" & goto :REPL_CHAR_NEXT
rem // Split the string at the next sequence of search characters:
for /F tokens^=1*^ delims^=^%CHR%^ eol^=^%CHR% %%S in ("!BUF!!INS!!WRK!") do (
rem // Store the portions before and after the character sequence:
endlocal & set "BUF=%%S" & set "TST=%%T" & set "WRK=%%T" & setlocal EnableDelayedExpansion
)
rem // Loop back and find the next character sequence:
set "INS=!RPL!" & goto :REPL_CHAR_LOOP
:REPL_CHAR_NEXT
rem // Return the resulting string with all special characters properly handled:
if not defined $NDX if defined BUF set "BUF=!BUF:"=""!^"
if not defined $NDX if defined BUF set "BUF=!BUF:^=^^^^!"
if not defined $NDX if defined BUF set "BUF=%BUF:!=^^^!%" !
if not defined $NDX if defined BUF set "BUF=!BUF:""="!^"
for /F "delims=" %%S in (^""!BUF!"^") do endlocal & endlocal & endlocal & set "%#RET%=%%~S" !
exit /B
repl_char_demo.bat
)如下:
>>> repl_char_demo.bat
In: some text,"&"&;0'@%~#`$:wild**card*?.re<dir>=|+([{parens}])-^/equal==to=!_
Out:some text,"&"&;0'@%~#`$:wild?card??.re<dir>=|+([{parens}])-^/equal==to=!_
In: some text,"&"&;0'@%~#`$:wild?card??.re<dir>=|+([{parens}])-^/equal==to=!_
Out:some text,"&"&;0'@%~#`$:wild?card??.re<dir>/|+([{parens}])-^/equal/to/!_
for /L
循环 遍历字符串中所有字符,检查每个字符是否与预定义字符相匹配并按指定方式替换的脚本。该方法替换每个匹配的单个字符而不是序列。再次将功能放入子例程中(此次忽略主要部分)::REPL_CHAR
::This function replaces in a string every occurrence of one certain character by another
::character or a string. It even correctly handles the characters `*` and `=`, as well as
::sequences of search characters so that every single one becomes replaced.
:: USAGE:
:: call :REPL_CHAR ref_output_string ref_input_string val_search_char val_replace_char
:: PARAMETERS:
:: ref_output_string reference to (name of) variable to receive the resulting string;
:: ref_input_string reference to variable that holds the original string; if empty
:: (`""`), the variable referenced by `ref_output_string` is used;
:: val_search_char single character that is to be replaced;
:: val_replace_char character or string to replace every single `val_search_char`
:: with; this may even be empty;
rem // Localise environment and detect whether delayed expansion is enabled (needed later):
setlocal & set "$NDX=!"
setlocal DisableDelayedExpansion
rem // Fetch arguments and verify them:
set "#RET=%~1" & if not defined #RET endlocal & endlocal & exit /B 2
set "#STR=%~2" & if not defined #STR set "#STR=%#RET%"
set "CHR=%~3"
if not defined CHR endlocal & endlocal & exit /B 1
set "RPL=%~4"
setlocal EnableDelayedExpansion
rem // Initialise several auxiliary variables:
set "WRK=!%#STR%!" & set "CHR=!CHR:~,1!" & set "BUF="
rem // Loop through all characters and check for match:
if defined WRK for /L %%J in (0,1,63) do for /L %%I in (0,1,127) do (
set /A "POS=%%J*64+%%I" & for %%P in (!POS!) do (
set "TST=!WRK:~%%P,1!" & if not defined TST goto :REPL_CHAR_QUIT
rem // Store character or replacement depending on whether there is a match:
if "!TST!"=="!CHR!" (set "BUF=!BUF!!RPL!") else (set "BUF=!BUF!!TST!")
)
)
:REPL_CHAR_QUIT
rem // Return the resulting string with all special characters properly handled:
if not defined $NDX if defined BUF set "BUF=!BUF:"=""!^"
if not defined $NDX if defined BUF set "BUF=!BUF:^=^^^^!"
if not defined $NDX if defined BUF set "BUF=%BUF:!=^^^!%" !
if not defined $NDX if defined BUF set "BUF=!BUF:""="!^"
for /F "delims=" %%S in (^""!BUF!"^") do endlocal & endlocal & endlocal & set "%#RET%=%%~S" !
exit /B
for /L
循环,而不是一个,只要到达字符串的结尾,两者都会被中断,使用goto
命令。中断for /L
循环意味着它在后台完成迭代,尽管其主体不再执行。因此,使用单个循环在中断后需要更长时间才能完成,而使用两个嵌套循环则不然。
>>> repl_char_demo.bat
In: some text,"&"&;0'@%~#`$:wild**card*?.re<dir>=|+([{parens}])-^/equal==to=!_
Out:some text,"&"&;0'@%~#`$:wild??card??.re<dir>=|+([{parens}])-^/equal==to=!_
In: some text,"&"&;0'@%~#`$:wild??card??.re<dir>=|+([{parens}])-^/equal==to=!_
Out:some text,"&"&;0'@%~#`$:wild??card??.re<dir>/|+([{parens}])-^/equal//to/!_
另一个解决上述问题的方法是在批处理脚本中使用PowerShell replace命令。
set var=*Love*
echo %var%>var.txt | powershell -command "((get-content var.txt) -replace '[\x2A]','x') -replace '.{1}$' | set-content var.txt"
set /p var=<var.txt
set var=%var%.m3u
echo %var%
if exist var.txt del var.txt
请参考这个答案,并使用set-ast.bat,在需要的文件中加入set-ast nam "x"
。
set-ast
接受参数<variable-to-modify> <string-to-replace-asterisks-with>