Page 1 of 1

Word with star character are ignored in FOR loop

Posted: 11 Jul 2011 13:24
by budhax
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]

Re: Word with star character are ignored in FOR loop

Posted: 11 Jul 2011 14:10
by aGerman
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

Re: Word with star character are ignored in FOR loop

Posted: 16 Jul 2011 10:11
by budhax
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.

Re: Word with star character are ignored in FOR loop

Posted: 16 Jul 2011 10:29
by aGerman
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