for f parameter loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

for f parameter loop

#1 Post by gunitinug » 17 Nov 2017 02:22

I have

weather.txt
-------------
January,Snowy,02
February,Rainy,15
March,Sunny,25

and

main.cmd
-----------

Code: Select all

@echo off
for /f "tokens=1,3 delims=," %%m in (weather.txt) do (echo %%m %%t)
I expect output:
January 02
February 15
March 25

But I get

January %t
February %t
March %t


Help THX

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

Re: for f parameter loop

#2 Post by dbenham » 17 Nov 2017 07:38

Why would you think %%t would work :?:
I see no logic in that :?

You specify %%m as the base character. Subsequent assignments simply follow the normal alpha sequence, so you should use %%m and %%n. (actually the sequence is based on Unicode code points, in case you use something other than ASCII letters)

Code: Select all

@echo off
for /f "tokens=1,3 delims=," %%m in (weather.txt) do (echo %%m %%n)
The skipped tokens do not impact the assigned symbol, and duplicate "tokens" values are ignored. The assignments are always done from left to right, from the standpoint of the input, regardless of the order of the "tokens" specification.

So the following would give the same result

Code: Select all

for /f "tokens=3,3,1,3,1 delims=," %%m in (weather.txt) do (echo %%m %%n)
Dave Benham

Post Reply