Echo "and* fails FOR %%a IN ("this" "and*" etc) DO ECHO %%a

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Echo "and* fails FOR %%a IN ("this" "and*" etc) DO ECHO %%a

#1 Post by alan_b » 24 Jan 2012 11:28

The code I wanted to use

Code: Select all

FOR %%a IN ("this" "and*" "that" "and??" "tother") DO ECHO %%a

unfortunately only three words were echoed
the wild cards "and*" and "and??" were not processed.
I tried without quotes, and with both slants of single quotes.
Is this a lost cause ?

At the moment I am using these 7 lines of code + 2 lines emphasizing continuity breaks.

Code: Select all

CALL :DoEach "this" "and*" "that" "and??" "tother"
GOTO SKIP

:DoEach
ECHO %1
SHIFT & IF "%2" NEQ "" GOTO NEEDIT
EXIT /B

:SKIP


N.B. The end result is actually not to echo the word,
but to execute much more complicated code I prefer not to explain in detail,
involving the use of and report on the presence or absence of executable's which may include a 64 suffix to denote 64 bit code,

Regards
Alan

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Echo "and* fails FOR %%a IN ("this" "and*" etc) DO ECHO

#2 Post by Ed Dyreen » 24 Jan 2012 11:40

'

Code: Select all

FOR /f "usebackq tokens=1-26 delims=¦" %%a IN ('"this"¦"and*"¦"that"¦"and??"¦"tother"') DO ECHO a=%%a, b=%%b, c=%%c, d=%%d, etc..

Code: Select all

a="this", b="and*", c="that", d="and??", etc..
Ofcourse, now you'll have problems with '¦' if it were to occur in data :(

Code: Select all

FOR /f "usebackq tokens=1-26 delims=" %%a IN ('"this" "and*" "that" "and??" "tother"') DO ECHO a=%%a, b=%%b, c=%%c, d=%%d, etc..

Code: Select all

a="this" "and*" "that" "and??" "tother", b=, c=, d=, etc..
But now the data is not delimited :(

Replacing '*' is really difficult, therefor I avoid it or use '\*' which is more easily replaced

Code: Select all

set "$=!$:\*=replace!"
Replacing the question mark is easier though.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Echo "and* fails FOR %%a IN ("this" "and*" etc) DO ECHO

#3 Post by alan_b » 24 Jan 2012 12:06

Sorry but no

1.
My illustration has each word on its own line, not as a series of words,
and "echo" is easily replaced with far more complicated code to process each word one at a time.

2.
If I find a need to add 9 more words to my list I do not want the aggravation of adding these precise supplementary items
%%f %%g %%h %%i %%j %%k %%l %%m %%n
I would much rather process 14 instances of %%a with each word in turn.

Regards
Alan

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Echo "and* fails FOR %%a IN ("this" "and*" etc) DO ECHO

#4 Post by aGerman » 24 Jan 2012 12:10

Hi Alan.

alan_b wrote:the wild cards "and*" and "and??" were not processed.

No, they were processed. Because of the wildcards files who match the pattern were searched (but obviously not found) ...

Workaround:

Code: Select all

set "a=*"
set "q=?"

setlocal EnableDelayedExpansion
FOR %%a IN ("this" "and^!a^!" "that" "and^!q^!^!q^!" "tother") DO ECHO %%a
endlocal

setlocal DisableDelayedExpansion
FOR %%a IN ("this" "and%%a%%" "that" "and%%q%%%%q%%" "tother") DO CALL ECHO %%a
endlocal

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Echo "and* fails FOR %%a IN ("this" "and*" etc) DO ECHO

#5 Post by dbenham » 24 Jan 2012 13:23

@ aGerman - nice :!:

The delayed expansion version also has an issue with ! and ^ in the string. And the escape sequence has to change depending on whether within quotes or not.

A few extra variables defined and an extra level of expansion makes the synax consistent and easy to read:

Code: Select all

@echo off

set "asterisk=*"
set "question=?"
set "exclaim=!"
set "caret=^"

set "a=!asterisk!"
set "q=!question!"
set "e=!exclaim!"
set "c=!caret!"

setlocal enableDelayedExpansion
for %%a in ("!a!" "!q!" "!e!" "!c!" !a! !q! !e! !c!) do echo %%a

Of course this is only good if we have control over the contents of the IN() clause. If the contents are from an external source, then we are out of luck. We can protect ? ! and ^ with search and replace. But * is a real problem. :(

Dave Benham

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Echo "and* fails FOR %%a IN ("this" "and*" etc) DO ECHO

#6 Post by alan_b » 24 Jan 2012 14:40

Many thanks.

I tried escaping with both a preceding ^ caret and also a preceding * asterisk,
and failed as I feared.

I am happy to say the "Big Guns" have taken care of my problem,
and I now have a much more elegant solution.

NB I have control over the text contained with the IN() clause.
and my only wild card requirement is to handle the presence or absence of the file
CCLEANER.EXE or
CCLEANER64.EXE or
the absence of both of them.

I now define SET "ast=*" and use "CCLEANER^!ast^!.EXE"

Regards
Alan

Post Reply