Solved

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

Posted on 2011-02-23
9
594 Views
Last Modified: 2012-08-13
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
Comment
Question by:bilokon
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Assisted Solution

by:t-max
t-max earned 52 total points
Comment Utility
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
 

Author Comment

by:bilokon
Comment Utility
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
 
LVL 6

Expert Comment

by:t-max
Comment Utility
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 52 total points
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 82

Accepted Solution

by:
oBdA earned 94 total points
Comment Utility
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
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
BTW in http:#34963218 it shouldn't have had the two ^ characters in there...

Steve
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 52 total points
Comment Utility
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
 

Author Comment

by:bilokon
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Thanks, glad you got a work around.

~bp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video discusses moving either the default database or any database to a new volume.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now