Link to home
Create AccountLog in
Avatar of Nate
NateFlag for United States of America

asked on

Dos "for" command question

When using the "FOR" command in a dos script, what are the options "Delims and "Tokens"  used for? and how do you use them?
Avatar of forrestoff
forrestoff

In short,
Delims: used to break the line up into chunks that you can reference with variables %i %j %k, etc
Tokens: used so you can tell the FOR command how many chunks you want to break it up into and how many will be handled by variables

Here's a more in-depth explanation:
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Lets say list.txt contains the following:

>type list.txt
My Name is David Booty
My Name is Shirly Jones
My name is Qunicy Adams

The FOR command would be a useful tool to output, for example:
David Booty
Shirly Jones
Quincy Adams

(we've stripped the "My Name" field away, and now you have a database of first and last names)

For the FOR command, it would take the following form:
>for /f %i in (list.txt) do @echo %i >> namedb.txt

This you hope would take the line and echo it to the prompt--but it doesn't.  what it gives you is teh following:

>type namedb.txt
My
My
My

The %i was only assigned to "My".  This is because the FOR command breaks the line up into chunks (parses it). %i is assigned to the first chunk.  without specifying delims, they default to space and tab.  For each line, we have 5 chunks because there are 5 parts seperated by spaces.

Now if we specify the following:

for /f "tokens=1,2,3,4,5" %i in (list.txt) do @echo %i %j %k %m %l >>named.txt

The following is the result

>type named.txt
My Name is Booty David
My Name is Jones Shirly
My name is Adams Qunicy

Notice how the first and last names changed places.  This is because we specified %l (which takes the first name) with %m (which takes the last name).

The default delims are still used and now the "tokens" specify which chunks to assign.  The FOR command increments the variables from the starting variable (i.e. %i).

Now if we had used:

  for /f "tokens=1*" %i in (list.txt) do @echo %i >>name.txt

name.txt would contain the following:

>type name.txt
My Name is David Booty
My Name is Shirly Jones
My name is Qunicy Adams

This is becuase the bits that are seperated by the space and tab (default delims) are all assigned to the first variable, or %i.

>for /f "tokens=1,2*" %i in (list.txt) do @echo %i RAAA!!!! %j
My RAAA!!!! Name is David Booty
My RAAA!!!! Name is Shirly Jones
My RAAA!!!! name is Qunicy Adams



Now, why delims?

well, what if list.txt contained the following:
>type list.txt
01/23/09 David Ferr 919-208-2288
01/23/03 Martha roberts 913-205-2233
01/22/01 amanda kern 945-654-7894

and what if you wanted to create a file like the following

>type desiredfile.txt
David Ferr was born 01-23-09 and you can reach him at (919) 208-2288.
Martha roberts was born ...

note the change in the date format and the phone number format.

>for /f "tokens=*" %i in (list.txt) do @echo %i
01/23/09 David Ferr 919-208-2288
01/23/03 Martha roberts 913-205-2233
01/22/01 amanda kern 945-654-7894

This produces the same output as just typing the file out (>type list.txt), but now lets seperate it.  We want to break it up into the following chunks:
%i = 01
%j = 23
%k = 09
%l = david
%m = Ferr
%n = 919
%o = 208
%p = 2288

Well, now we need to indicate that "/" and "-" are to be used as delimiters.  So we have the following:

>for /f "tokens=1,2,3,4,5,6,7,8 delims=/- " %i in (list.txt) do @echo %i %j %k %l %m %n %o %p
01 23 09 David Ferr 919 208 2288
01 23 03 Martha roberts 913 205 2233
01 22 01 amanda kern 945 654 7894

Notice the space after the "-" in the delims= parameter. So our desired format can be realized by the following:

>for /f "tokens=1,2,3,4,5,6,7,8 delims=/- " %i in (list.txt) do @echo %l %m was born %i-%j-%k and you can reach him at (%n) %o-%p.
David Ferr was born 01-23-09 and you can reach him at (919) 208-2288.
Martha roberts was born 01-23-03 and you can reach him at (913) 205-2233.
amanda kern was born 01-22-01 and you can reach him at (945) 654-7894.

I hope this helps.

Her's a pointer to another example:
http://www.robvanderwoude.com/ntfortokens.html
Another example:

The "for" command applied  to the list files and folders.

Let's say we have a folder named "test_for" ->C:\Documents and Settings\[user]\Documents\test_for

In this folder you have four files:

- doc1.txt
- doc2.txt
- doc3.txt
- doc4.txt

Let's say you need to split the file name from the extension

- You can use command:
for /f "tokens=1,2 delims=." %a in ('dir /b *') do  @echo %a
Output:

doc1
doc2
doc3
doc4

- You can add a second token and a second %var :
for /f "tokens=1,2 delims=." %a in ('dir /b *') do  @echo %a %b
Output:

doc1 txt
doc2 txt
doc3 txt
doc4 txt

- You can use command:
for /f "tokens=1,2 delims=." %a in ('dir /b *') do  @echo %b
Output:

txt
txt
txt
txt

- You can add a third token,a second delims(in this example char "c") and a third %var :
for /f "tokens=1,2,3 delims=c,." %a in ('dir /b *') do  @echo %a %b %c
Output:

do 1 txt
do 2 txt
do 3 txt
do 4 txt

- You can merge the char "c" to the var %a with this command : for /f "tokens=1,2,3 delims=c,." %a in ('dir /b *') do  @echo %ac %b %c
Output:

doc 1 txt
doc 2 txt
doc 3 txt
doc 4 txt