Solved

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

Posted on 2011-02-23
9
602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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 85

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 56

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 56

Expert Comment

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

~bp
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

628 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