Page 1 of 1

Words with star char are ignored in FOR loop

Posted: 07 May 2017 12:55
by budhax
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.

Re: Words with star char are ignored in FOR loop

Posted: 07 May 2017 13:38
by aGerman
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

Re: Words with star char are ignored in FOR loop

Posted: 07 May 2017 15:09
by budhax
It works fine. Problem solved. Thanks a lot :)

Re: Words with star char are ignored in FOR loop

Posted: 07 May 2017 17:06
by Thor
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

Re: Words with star char are ignored in FOR loop

Posted: 08 May 2017 07:17
by budhax
Shorter solution. Thanks again.

Re: Words with star char are ignored in FOR loop

Posted: 08 May 2017 10:28
by aGerman
budhax wrote:Shorter solution.

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

Steffen