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

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

问题描述

当在Windows批处理文件运行一切以下code含有星号,这是跳过字符串的工作放在一边。检查通过数字传递的参数(即回声(%〜6 ),我可以看到星号 - 传递给时FOR循环,我有一个问题,它只是:

 关闭@echo
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
拨打:CMD的毗连这是一个演示的CONCAT功能。 希望这会工作; 但事实并非如此,当我传递,*星号,人物
回音!CMD!@goto:结束
@goto:EOF
:的毗连
::连接字符串的一个给定的列表,但不包括其报价
:: 1 - 输出变量
:: 2 * - 字符串Concat的
回声(%*
集/年XX = 0
设置Concat_tempFlag = 0
设置Concat_temp =
对于%%的(%*)在做(
    集/年XX =!XX!+1
    回音!XX! - %%一个
    如果!Concat_tempFlag!== 1(
        设置Concat_temp =!Concat_temp!%%〜一
    )其他(
        设置Concat_tempFlag = 1
    )

集%〜1 =%Concat_temp%
@goto:EOF:结束
回声(再见
退出/ B 0

我已经尝试 FOR / F(令牌= *)%% A有('回声(%*')做(如下建议:的Batch FOR用星号(及其变型),但没有运气循环。任何想法?在此先感谢。


解决方案

在这里找到解决方案:<一href=\"http://stackoverflow.com/questions/11685375/i-need-to-match-or-replace-an-asterisk-in-a-batch-environmental-variable-using\">I需要匹配或使用唯一的本地Windows命令在批处理环境变量替换星号*。这可能吗?

全部code如下:

 关闭@echo
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
设置DEFAULT_AsteriskMarker = _xAsteriskMarkerx_
拨打:CMD的毗连这是一个演示的CONCAT功能。 希望这会工作; 但事实并非如此,当我传递,*星号,人物
回音!CMD!@goto:结束
@goto:EOF
:的毗连
::连接字符串的一个给定的列表,但不包括其报价
:: 1 - 输出变量
:: 2 * - 字符串Concat的
设置Concat_StringsToConcat =%*
回声(%Concat_StringsToConcat%
拨打:AsteriskFix Concat_StringsToConcat
集/年XX = 0
设置Concat_tempFlag = 0
设置Concat_temp =
对于%%的(%Concat_StringsToConcat%)在做(
    集/年XX =!XX!+1
    回音!XX! - %%一个
    如果!Concat_tempFlag!== 1(
        设置Concat_temp =!Concat_temp!%%〜一
    )其他(
        设置Concat_tempFlag = 1
    )

!集%〜1 =Concat_temp:%DEFAULT_AsteriskMarker%= *!
@goto:EOF:AsteriskFix
::http://stackoverflow.com/questions/11685375/i-need-to-match-or-replace-an-asterisk-in-a-batch-environmental-variable-using
设置AsteriskFix_temp =!%〜1!
如果%〜2==(
    设置AsteriskFix_marker =%DEFAULT_AsteriskMarker%
)其他(
    设置AsteriskFix_marker =%〜2

拨打:strlen的AsteriskFix_temp AsteriskFix_len
对/ L %%倍In(0,1%AsteriskFix_len%)做如果不是AsteriskFix_temp:〜!%% X,1!==如果AsteriskFix_temp:!〜%% X,1== *(
    集/ A AsteriskFix_plusone = %% X + 1
    在/ L %% Y(!AsteriskFix_plusone!1!AsteriskFix_plusone!)做(
        设置AsteriskFix_temp = AsteriskFix_temp:〜!0 %% X%AsteriskFix_marker%AsteriskFix_temp:!〜%% Y!
    )

集%〜1 =!AsteriskFix_temp!
@goto:EOF:STRLEN
:: HTTP://www.dostips.com/Dt$c$cCmdLib.php#strLen
设置StrLen_str = A!%〜1! &安培; ::保持一个达阵,以确保我们得到的长度,而不是上限
                                 ::这也避免了空字符串的情况下麻烦
设置StrLen_len = 0
在/ L %% A(12,-1,0)做(
    集/ AStrLen_len | = 1&LT;&LT; %% A
    在做,如果%%乙:==设置/ AStrLen_len&安培; =〜1&LT;&LT; %% A(StrLen_len!)StrLen_str〜%% B,1!

IF%〜2NEQ,SET / A%〜2 =%StrLen_len%
@goto:EOF:结束
回声(再见
退出/ B 0

由于詹姆斯氏/ P>

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

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.

解决方案

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?

Full code below:

@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
::http://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

Thanks to James K

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

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