Using a delayed expansion variable as a token in a FOR command (for /f "tokens=!var!)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Using a delayed expansion variable as a token in a FOR command (for /f "tokens=!var!)

#1 Post by Cornbeetle » 20 Oct 2016 14:00

Why do I get the "!opt! was unexpected at this time" error for this basic example below? This only seems to fail when using delayed expansion, but if I do a single FOR command with no /l looping and use %opt% it works fine...

Code: Select all

for /l %%U in (1,1,3) do (
   set /a rand=!random!%%7+1
   set "opt=tokens=!rand!"
   for /f "!opt!" %%G in ("a b c d e f g") do (
      set /a i+=1
      set char[!i!]=%%G
   )
)

echo %char[1]%-%char[2]%-%char[3]%
pause


I expect to get these results for example

Code: Select all

 e-g-f 


Any known issues with what I'm trying?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Using a delayed expansion variable as a token in a FOR command (for /f "tokens=!var!)

#2 Post by dbenham » 20 Oct 2016 14:16

Yep, absolutely it is a known issue.

You cannot used delayed expansion or FOR variables as FOR options or IF options. The options are parsed and interpreted before delayed expansion and FOR variable expansion takes place, so it can't work.


Dave Benham


pieh-ejdsch
Posts: 246
Joined: 04 Mar 2014 11:14
Location: germany

Re: Using a delayed expansion variable as a token in a FOR command (for /f "tokens=!var!)

#4 Post by pieh-ejdsch » 20 Oct 2016 15:09

You can use

Code: Select all

Cmd /s /c(for /d %x %i in ( *.*) do ...)

https://www.administrator.de/wissen/ordner-suchen-158812.html#toc-8

Phil

pieh-ejdsch
Posts: 246
Joined: 04 Mar 2014 11:14
Location: germany

Re: Using a delayed expansion variable as a token in a FOR command (for /f "tokens=!var!)

#5 Post by pieh-ejdsch » 23 Oct 2016 05:04

This will work
with Var or ForVar

Code: Select all

@echo off
setlocal
for /L %%L in (1 1 3) do (
  call set /a n=%%random%%
  set /a N=n%%7+1
  for /f delims^= %%C in ('for /f "tokens=%%N%%" %%x in ^("a b c d e f g"^) do @echo %%x') do (
    set "char[%%L]=%%C"
  )
)
set char
echo %char[1]%-%char[2]%-%char[3]%-%char[5]%
pause

@echo off
setlocal
for /L %%L in (1 1 3) do (
  for /f delims^= %%N in ('set /a %%random%% %%7 +1') do (
    for /f delims^= %%C in ('for /f "tokens=%%N" %%x in ^("a b c d e f g"^) do @echo %%x') do (
      set "char[%%L]=%%C"
    )
  )
)


set char
echo %char[1]%-%char[2]%-%char[3]%-%char[5]%
pause


Phil

Cornbeetle
Posts: 31
Joined: 23 Nov 2015 09:34

Re: Using a delayed expansion variable as a token in a FOR command (for /f "tokens=!var!)

#6 Post by Cornbeetle » 25 Oct 2016 08:25

Nice, this one works great, thank you Phil!

@echo off
setlocal
for /L %%L in (1 1 3) do (
call set /a n=%%random%%
set /a N=n%%7+1
for /f delims^= %%C in ('for /f "tokens=%%N%%" %%x in ^("a b c d e f g"^) do @echo %%x') do (
set "char[%%L]=%%C"
)
)
set char
echo %char[1]%-%char[2]%-%char[3]%-%char[5]%
pause

Post Reply