Word with star character are ignored in FOR loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

Word with star character are ignored in FOR loop

#1 Post by budhax » 11 Jul 2011 13:24

Hello,
This FOR loop ignores (jump over) strings including star character: *

SET d=aaa* file*.* file3.txt
FOR %%f in (%d%) DO (ECHO.%%~f)
FOR %%f in ("%d: =" "%") DO (ECHO.%%~f)


The output I got with this script is only the 3rd string: file3.txt
I would like to get this:
aaa*
file*.*
file3.txt


How tho solve my problem?
Thanks and regards.
CONFIG:
MS Windows 7 SP1, Firefox 5. Microsoft Windows [Version 6.1.7601]

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Word with star character are ignored in FOR loop

#2 Post by aGerman » 11 Jul 2011 14:10

The asterisk is a wildcard character. The FOR loop is trying to find files which match to the pattern aaa* or file*.* in the current environment.
Try SET d=* and figure out what happens.

A possible solution is a FOR /F loop:

Code: Select all

SET d=aaa* file*.* file3.txt
FOR /f "tokens=1-3" %%f in ("%d%") DO (ECHO.%%f&ECHO.%%g&ECHO.%%h)


Regards
aGerman

budhax
Posts: 63
Joined: 09 Oct 2006 12:25

Re: Word with star character are ignored in FOR loop

#3 Post by budhax » 16 Jul 2011 10:11

Thank you aGerman. I prefer the solutions proposed there:
http://ss64.org/viewtopic.php?pid=4901
because their are independent of the number of item/word defined in the string d.

I mean, solutions in ss64.org work for:

Code: Select all

::String d contains 3 items
SET d=aaa* file*.* file3.txt
or
::String d contains 5 items
SET d=file fileB.* blabla file4*.* gag.txt


Thanks and regards.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Word with star character are ignored in FOR loop

#4 Post by aGerman » 16 Jul 2011 10:29

I see. A similar solution:

Code: Select all

@echo off &setlocal
SET d=aaa* file*.* file3.txt
call :proc_args %d%
pause
goto :eof

:proc_args
if "%~1"=="" goto :eof
echo %~1
shift
goto :proc_args

Regards
aGerman

Post Reply