views:

192

answers:

3

This is my code so far:

for /f "tokens=1 eol=," %%f IN ("1,2,3,4") do  (
    echo .
    echo %%f    
)

I'm expecting that to produce:

.
1
.
2
.

etc...

But instead I get:

.
1

And that's it. What am I missing?

+1  A: 

Try this:

set test=1,2,3,4
for /d %%f IN (%test%) do echo %%f
Rubens Farias
This does work.
jeffamaphone
Thanks, that works too.
jcollum
+2  A: 

You've misunderstood the options.

  • tokens=1 means you only want the first token on each line. You want all of the tokens on the line.
  • eol=, means you want to interpret a comma as the beginning of an end of line comment. You want to use delims=, instead to indicate the comma is the delimiter (instead of the default value of whitespace).

FOR /F is primarily for operating on lines in a file. You're not doing that. You're operating on a single string, so Rubens' answer is closer to what you want:

@ECHO OFF
SET test=1,2,3,4
FOR /D %%F IN (%test%) DO (
  ECHO .
  ECHO %%F
)

However, in theory, you should be able to say something like:

FOR /F "usebackq delims=, tokens=1-4" %%f IN ('1^,2^,3^,4') DO (
  ECHO .
  ECHO %%f    
  ECHO .
  ECHO %%g
  ECHO .
  ECHO %%h
  ECHO .
  ECHO %%i
)

This works as well, but probably doesn't scale in the way you want. Note that you have to escape the comma in the string using the ^ character, and you have to specify the tokens you want and then use the subsequent variables %g, %h and %i to get them.

jeffamaphone
"You've misunderstood the options": this doesn't suprise me. The documentation that I found wasn't that great.
jcollum
But if I tell it that comma is the end of line, don't I then only want the first token on each line? I tried using the delims option as well and still didn't get what I wanted. Oh well.
jcollum
Yeah, it sucks. FOR /? has to be read very carefully. This is another source: http://technet.microsoft.com/en-us/library/bb490909.aspx
jeffamaphone
eol doesn't mean end of line. It means everything after the comma is an end of line comment. Including the remaining commas.
jeffamaphone
eol is so you can parse files like hosts files where ; is used as a comment character, but other file formats use @ as a comment, etc. This whole things is cobbled together from spit and duct tape. That's why PowerShell is the new hotness.
jeffamaphone
Jeez yeah, I wish PowerShell was an option for this thing I need to do.
jcollum
A: 

@OP, and while you are learning how to use DOS batch scripting, you might want to learn vbscript (or powershell) as well. These are alternatives and they make your batch scripting easier, especially when it comes to more complex tasks.

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strInput = objArgs(0)
s = Split(strInput,",")
For Each i In s
 WScript.Echo i
Next

save the above as mysplit.vbs and on command line

C:\test>cscript //nologo mysplit.vbs 1,2,3,4
1
2
3
4
ghostdog74
both good options, but unfortunately they'd require approval and we're on a deadline; once deadline is done I plan on ditching bat files in a hurry and going to Nant for this task.
jcollum