2013-06-12 182 views
3

在Windows批處理文件中運行以下代碼時,除了包含星號的字符串外,所有操作都將被忽略。檢查通過數字傳遞的參數(即echo(%~6)我可以看到星號 - 傳遞給時FOR循環,我有一個問題,它只是:在Windows批處理文件的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 (的建議在這裏:Batch FOR loop with asterisk(和變化)但沒有運氣。有任何想法嗎?提前致謝。

+1

我沒有答案給你,但一個很好的效果[這裏](http://pastebin.com/965KhAME)。如你所知,答案已經[在這裏](http://stackoverflow.com/a/13617328/2098699)。 – Endoro

回答

5

找到了解決辦法在這裏: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 

感謝詹姆斯ķ

4

您提供通向正確答案的鏈接:

有沒有辦法保持在設定正常(無/ F選項)用於指揮一個星號(也沒有問號)(它們總是改爲文件名);您需要在FOR/F命令中分隔參數。如果你也想處理在for循環的每個參數,然後第二次爲不能在同樣的情況下,所以你必須調用一個子程序可以更改上下文

+1

謝謝驗證碼 - 我有一個不好的習慣,就是忽略回答,說不/總是相信有一個解決方法。經過一些研究後,我在這裏找到了另一個解決方法 - 不是最好的,但需要。大聲說出你的答案,嚴格來說你是對的 - 沒有完美的方式來做這件事。 – JohnLBevan