Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

DOS/Windows batch for loop: strange behaviour in the presence of wildcards

I'm testing this code:

set THINGS=a.dll b.dll c-*-a.dll d-*-a.dll e.dll

setlocal EnableExtensions EnableDelayedExpansion
for %%a in (%THINGS%) do (
    echo %%a
)
endlocal

Open in new window


I would like it to display

a.dll
b.dll
c-*-a.dll
d-*-a.dll
e.dll

Open in new window


(I do need to use those wildcards, but later on.) Instead I see this:

a.dll
b.dll
e.dll

Open in new window


If there are files in the current directory (which in this case is completely irrelevant) matching c-*-a.dll and d-*-a.dll, then those files names are displayed, otherwise I get nothing for those two. However, this is not what I want at all.

Is there any way to get this to work as I described and escape the wildcards one way or another? It seems to be one of the numerous design flaws in the batch scripting language. Is it possible to overcome it? I tried escaping the wildcards: ^*. I also tried other approaches, none seem to work.

I would really appreciate your help!

Many thanks,
Paul
0
bilokon
Asked:
bilokon
  • 2
  • 2
  • 2
  • +2
4 Solutions
 
t-maxCommented:
OK, here's the batch that does it, but in a different way because it's not feasible the way you tried to pursue it.
C:\>type max2.bat
@ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
   echo %1
SHIFT
GOTO Loop
:Continue


C:\>max2.bat a.dll b.dll c-*-a.dll d-*-a.dll e.dll
a.dll
b.dll
c-*-a.dll
d-*-a.dll
e.dll

Open in new window


So if you can pass the %THINGS% to a batch file as a parameter (which should be easy using another batch that does "CALL max2.bat %THINGS%") then will work.

The problem here is that whatever you put in parenthesis in the "for" command, will be interpreted. I tried escaping it with caret, but that didn't work for me. At least this trick does the job.
0
 
bilokonAuthor Commented:
Hi t-max,

Many thanks for this idea. Is there any way to avoid having two batch files? (Path resolution is a problem for various reasons.) E.g. could SHIFT operate on another environment variable? Or perhaps I could fake the command line arguments and initialise them from an environment variable?

Many thanks,
Paul
0
 
t-maxCommented:
Well, you don't really need 2 batch, just the one I provided. You can call it from cmd without problems.
Of course you can have a variable like THINGS set in the environment, and then you can do:
max2.bat %THINGS%
It will give the same output as above.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Steve KnightIT ConsultancyCommented:
Prbably other ways but only passing quickly.  You could do it with a call to another subroutine:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
call :THINGS a.dll b.dll "c-^*-a.dll" "d-^*-a.dll" e.dll

goto :eof

:THINGS
echo %~1
shift
if not "%~1"=="" goto :things
0
 
oBdACommented:
If you can live with two different environment variables (you only need to define one of them manually), you can tweak it.
You'll have to use a "/" (or another character that doesn't appear in any of the names, but since these appear to be file names, and a "/" can't be used in a file name, it sort of recommends itself) instead of the asterisk to define a "THINGS" list that is used for the echo. The "/" can then be replaced on the fly with an *, and the "normal" "THINGS" variable can automatically be constructed from it as well. (Unfortunately, you can't use the asterisk as "source" character in set's character substitution, it only works the other way).

@echo off
set THINGS_ECHO=a.dll b.dll c-/-a.dll d-/-a.dll e.dll
set THINGS=%THINGS_ECHO:/=*%
echo THINGS: %THINGS%
setlocal EnableExtensions EnableDelayedExpansion
for %%a in (%THINGS_ECHO%) do (
  set a=%%a&echo !a:/=*!
)
endlocal

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
BTW in http:#34963218 it shouldn't have had the two ^ characters in there...

Steve
0
 
Bill PrewCommented:
Just to throw a slightly different approach out there:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set T1=a.dll
set T2=b.dll
set T3=c-*-a.dll
set T4=d-*-a.dll
set T5=e.dll
for /L %%A in (1,1,5) do (
  echo !T%%A!
)
endlocal

Open in new window

~bp
0
 
bilokonAuthor Commented:
Many thanks for these suggestions. All are very interesting, but in this particular case I would use oBdA's approach but I'd rather use something more wildcard-like looking as a wildcard:

@echo off
set THINGS_ECHO=a.dll b.dll c-$-a.dll d-$-a.dll e.dll
set THINGS=%THINGS_ECHO:$=*%
echo THINGS: %THINGS%
setlocal EnableExtensions EnableDelayedExpansion
for %%a in (%THINGS_ECHO%) do (
  set a=%%a&echo !a:$=*!
)
endlocal

Open in new window

0
 
Bill PrewCommented:
Thanks, glad you got a work around.

~bp
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now