Batch file: count tokens in var

?
Score
0
Vote Up
March 2, 2008 at 08:24:35 Pacific
Specs: XP, 1024

I would like to be able to count the number of words in a variable that holds between 1 and 10 words delimited by a space.

For example:
Set VAR1=word1 word2 word3

FOR /f "tokens=* delims= " %%a in (%VAR1%) do set /a count+=1

echo %count% (3 in this case)

Thanks in advance



Reply ↓  Report •


#1
Vote Down
Score
0
Vote Up
March 2, 2008 at 08:51:07 Pacific

Set VAR1=word1 word2 word3
Set count=0
For %%j in (%VAR1%) Do Set /A count+=1
Echo.%count%


Reply ↓  Report •

#2
Vote Down
Score
0
Vote Up
March 2, 2008 at 09:20:46 Pacific

Thanks that works like a charm.
I knew I was close ;-)


Reply ↓  Report •

Reply to Message Icon Start New Discussion
Related Posts

« XCopy /v question create batch file »

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC