在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
我已经尝试过这个建议:Batch FOR loop with asterisk(以及其变体),但都没有成功。有什么想法吗?谢谢。
for /F (tokens=*) %%A in ('echo(%*') do (