在Windowsbatch file中运行以下代码时,除了包含星号的string外,其他所有内容都将被忽略。 通过数字检查传递的参数(即echo(%~6
)我可以看到星号 – 只有当传递给FOR循环,我有一个问题:
@echo off setlocal enableextensions enabledelayedexpansion call:Concat cmd "this is a demo" " of concat functionality." " Hopefully it will work;" " but it doesn't when I pass an" " * asterisk" " character" echo !cmd! @goto:end @goto:eof :Concat ::Concatenates a given list of strings without including their quotes ::1 - output variable ::2* - strings to concat echo(%* set /a xx=0 set Concat_tempFlag=0 set Concat_temp= for %%A in (%*) do ( set /a xx=!xx!+1 echo !xx! - %%A if !Concat_tempFlag!==1 ( set Concat_temp=!Concat_temp!%%~A ) else ( set Concat_tempFlag=1 ) ) set "%~1="%Concat_temp%"" @goto:eof :End echo(Bye exit /b 0
我尝试for /F (tokens=*) %%A in ('echo(%*') do (
如在这里build议: 批处理循环与星号 (及其变体),但没有运气。提前。
在这里找到解决方案: 我需要使用本地Windows命令匹配或替换批处理环境变量中的星号*。 这可能吗?
下面的完整代码:
@echo off setlocal enableextensions enabledelayedexpansion set DEFAULT_AsteriskMarker=_xAsteriskMarkerx_ call:Concat cmd "this is a demo" " of concat functionality." " Hopefully it will work;" " but it doesn't when I pass an" " * asterisk" " character" echo !cmd! @goto:end @goto:eof :Concat ::Concatenates a given list of strings without including their quotes ::1 - output variable ::2* - strings to concat set Concat_StringsToConcat=%* echo(%Concat_StringsToConcat% call:AsteriskFix Concat_StringsToConcat set /a xx=0 set Concat_tempFlag=0 set Concat_temp= for %%A in (%Concat_StringsToConcat%) do ( set /a xx=!xx!+1 echo !xx! - %%A if !Concat_tempFlag!==1 ( set Concat_temp=!Concat_temp!%%~A ) else ( set Concat_tempFlag=1 ) ) set "%~1="!Concat_temp:%DEFAULT_AsteriskMarker%=*!" @goto:eof :AsteriskFix ::https://stackoverflow.com/questions/11685375/i-need-to-match-or-replace-an-asterisk-in-a-batch-environmental-variable-using set AsteriskFix_temp=!%~1! if "%~2"=="" ( set AsteriskFix_marker=%DEFAULT_AsteriskMarker% ) else ( set AsteriskFix_marker=%~2 ) call:StrLen AsteriskFix_temp AsteriskFix_len for /l %%x in (0,1,%AsteriskFix_len%) do if not "!AsteriskFix_temp:~%%x,1!"=="" if "!AsteriskFix_temp:~%%x,1!"=="*" ( set /a AsteriskFix_plusone=%%x+1 for /l %%y in (!AsteriskFix_plusone!, 1, !AsteriskFix_plusone!) do ( set AsteriskFix_temp=!AsteriskFix_temp:~0,%%x!%AsteriskFix_marker%!AsteriskFix_temp:~%%y! ) ) set "%~1=!AsteriskFix_temp!" @goto:eof :StrLen ::http://www.dostips.com/DtCodeCmdLib.php#strLen set "StrLen_str=A!%~1!" &:: keep the A up front to ensure we get the length and not the upper bound ::it also avoids trouble in case of empty string set "StrLen_len=0" for /L %%A in (12,-1,0) do ( set /a "StrLen_len|=1<<%%A" for %%B in (!StrLen_len!) do if "!StrLen_str:~%%B,1!"=="" set /a "StrLen_len&=~1<<%%A" ) IF "%~2" NEQ "" SET /a %~2=%StrLen_len% @goto:eof :End echo(Bye exit /b 0
感谢James K
您提供的链接导致正确的答案:
在正常(无/ F选项)FOR命令的集合(它们总是更改为文件名)中没有办法保留星号(也不是问号)。 您需要在FOR / F命令中分隔参数。 如果您还想在FOR循环中处理每个参数,则第二个FOR不能位于同一个上下文中,因此您必须调用子例程来更改上下文