Solved

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

Posted on 2011-02-23
9
597 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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

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 53

Expert Comment

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

~bp
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

14 Experts available now in Live!

Get 1:1 Help Now