4

在 Windows 批处理文件中运行以下代码时,除了包含星号的字符串(被跳过)之外,一切正常。按数字检查传递的参数(即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 (按照这里的建议尝试过:带有星号(及其变体)的批处理 FOR 循环,但没有运气。有任何想法吗?提前致谢。

4

2 回答 2

6

在此处找到解决方案:我需要仅使用本机 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

感谢詹姆斯 K

于 2013-06-13T12:41:09.190 回答
4

您提供的链接导致正确答案:

无法在普通(无 /F 选项)FOR 命令的集合中保留星号(也不是问号)(它们总是更改为文件名);您需要在 FOR /F 命令中分隔参数。如果你还想在一个 FOR 循环中处理每个参数,那么第二个 FOR 不能在同一个上下文中,所以你必须 CALL 一个子程序来改变上下文

于 2013-06-12T18:26:01.983 回答