Solved

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

Posted on 2011-02-23
9
595 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
ID: 34961150
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
ID: 34961571
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
ID: 34962305
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
ID: 34963218
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 83

Accepted Solution

by:
oBdA earned 94 total points
ID: 34965242
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
ID: 34966013
BTW in http:#34963218 it shouldn't have had the two ^ characters in there...

Steve
0
 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 52 total points
ID: 34966777
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
ID: 34970983
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 52

Expert Comment

by:Bill Prew
ID: 34971040
Thanks, glad you got a work around.

~bp
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

11 Experts available now in Live!

Get 1:1 Help Now