4

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.

Community
  • 1
  • 1
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • 1
    I have no answer for you, but a nice effect [here](http://pastebin.com/965KhAME). The answer is already [here](http://stackoverflow.com/a/13617328/2098699), as you know. – Endoro Jun 12 '13 at 17:54

2 Answers2

6

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
::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

Thanks to James K

Community
  • 1
  • 1
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
4

The link you provided leads to the right answer:

There is no way to preserve an asterisk (nor a question mark) in the set of a normal (no /F option) FOR command (they are always changed to file names); you need to separate the parameters in a FOR /F command. If you also want to process each parameter in a FOR loop, then the second FOR can NOT be in the same context, so you must CALL a subroutine to change the context

captcha
  • 3,756
  • 12
  • 21
  • 1
    Thanks Captcha - I have a bad habit of ignoring answers that say no / always believe there's a workaround. After a bit of research I found another answer on here with a fix - not the neatest, but needs must. Upvoted your answer as strictly speaking you are correct - there's no neat way of doing this. – JohnLBevan Jun 13 '13 at 12:44