在Windows批处理文件的FOR循环中转出星号 [英] Escape asterisk in Windows Batch File's FOR Loop

查看:154
本文介绍了在Windows批处理文件的FOR循环中转出星号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows批处理文件中运行以下代码时,除了包含星号的字符串之外,所有内容都将被跳过。通过数字检查传递的参数(即 echo(%〜6 )我可以看到星号 - 只有当传递给FOR循环时,我有一个问题:

When running the following code in a windows batch file everything works aside from the string containing the asterisk, which is skipped. Checking the passed parameters by number (i.e. echo(%~6) I can see the asterisk - it's only when passed to the FOR loop that I have an issue:

@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循环(及其变体),但没有运气。有任何想法吗?感谢提前。

I've attempted for /F (tokens=*) %%A in ('echo(%*') do ( as suggested here: Batch FOR loop with asterisk (and variations thereof) but with no luck. Any ideas? Thanks in advance.

推荐答案

在这里找到解决方案:我需要使用本地Windows命令在批量环境变量中匹配或替换星号*。这是可能吗?

Found the solution here: I need to match or replace an asterisk * in a batch environmental variable using only native Windows commands. Is this possible?

完整代码如下:

@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

这篇关于在Windows批处理文件的FOR循环中转出星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆