Words with star char 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

Words with star char are ignored in FOR loop

#1 Post by budhax » 07 May 2017 12:55

Hello,
This FOR loop ignores/passes the items containing the star char *.

Code: Select all

set list=coo*.* ses*.js sign bookmarkbackups\*.json
for %%e in (%list%) do (echo.%%~e)

So, the output is:

Code: Select all

sign

Which loop can output all items (separated by a space)?

Code: Select all

coo*.*
ses*.js
sign
bookmarkbackups\*.json


Thanks and regards.

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

Re: Words with star char are ignored in FOR loop

#2 Post by aGerman » 07 May 2017 13:38

Asterisks are wild cards that lead to search for files that match the pattern.

You could replace the spaces with line feed characters. That way you can process the list in a FOR /F loop.

Code: Select all

@echo off &setlocal
set "list=coo*.* ses*.js sign bookmarkbackups\*.json"

set ^"lf=^
%= make sure there are no spaces at the end of the above and this line =%
^"

set "list=%list:^=^^%"
set "list=%list:!=^^^!%"
setlocal EnableDelayedExpansion
set "list=%list: =" & set "list=!list!!lf!%"

for /f "delims=" %%i in ("!list!") do echo %%i

pause

Steffen

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

Re: Words with star char are ignored in FOR loop

#3 Post by budhax » 07 May 2017 15:09

It works fine. Problem solved. Thanks a lot :)

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Words with star char are ignored in FOR loop

#4 Post by Thor » 07 May 2017 17:06

You could try this also:

Code: Select all

@echo off &setlocal enableDelayedExpansion
set "list=coo*.* ses*.js sign bookmarkbackups\*.json"
for /l %%i in (1 1 4) do (
  for /f "tokens=1* delims= " %%A in ("!list!") do (
  echo %%A
  set "list=%%B"
  )
)
endlocal &exit /b

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

Re: Words with star char are ignored in FOR loop

#5 Post by budhax » 08 May 2017 07:17

Shorter solution. Thanks again.

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

Re: Words with star char are ignored in FOR loop

#6 Post by aGerman » 08 May 2017 10:28

budhax wrote:Shorter solution.

Yes absolutely sufficient as long as the number of tokens (used in the FOR /L loop) is already known.

Steffen

Post Reply