Page 1 of 1

Why do you need "tokens" and "delims" in "for"

Posted: 08 Oct 2011 21:49
by Rileyh
Hi,
I don't understand why you need to put in "tokens" and "delims" in the "for" command.
Could someone tell me why and provide explanations for it?

Any help would be appreciated,
Rileyh

Re: Why do you need "tokens" and "delims" in "for"

Posted: 09 Oct 2011 09:55
by trebor68
Here a example code:

Code: Select all

@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1-3 delims=#" %%r in (Test21.txt) do (
  set name=%%r
  set name=!name:~0,-2!
  echo name: "!name!"  -%%t
  for %%c in (%%s) do echo folder: %%c
  echo.
)


And here the file "Test21.txt":

Code: Select all

name1  #  01 05 10  #  info1
name2_1 name2_2  #  01 08 10  #  info2
name3  #  01 06  #  info3
name4  #  05 07 08  #  info4_1 info4_2
name5_1 name5_2  #  06  #  info5


The program will use the three token areas. When we use the first row will set to the tokens number 1 to 3.
Token number 1 is "name1 "
Token number 2 is " 01 05 10 "
Token number 3 is " info1"

With the parameter "tokens=1-3" and the variable "%%r" is following.
The token one is %%r, the token two is %%s and token three is %%t.

Please let run the code.

When you dont use the "delims" then is the delimeter only the space. It is also possible that you can use more then one letter. Here the example "delims=# " this will use the letters "#" and the space.

Here a example for token number 1 and 3.

Code: Select all

@echo off
for /f "tokens=1,3 delims=#" %%r in (Test21.txt) do (
  echo name: %%r-%%s
  echo.
)