String split with separaotr

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kameha
Posts: 1
Joined: 11 Jun 2010 01:37

String split with separaotr

#1 Post by kameha » 11 Jun 2010 01:43

hi there !

I've got the following script:

Code: Select all

SET BASES_NAMES=Base1,Base2

FOR /F "tokens=1 delims=," %%a IN ("%BASES_BTT%") DO (
   ECHO Valeur: %%a
)


I would like to produce the following output:

Valeur: Base1
Valeur: Base2

But can only have the first line... why ?

please help !

Barty
Posts: 3
Joined: 02 Jun 2010 04:53

Re: String split with separaotr

#2 Post by Barty » 11 Jun 2010 05:55

You need to change tokens to 1-2
You need to write the right var
You need to echo %%b

SET BASES_NAMES=Base1,Base2

FOR /F "tokens=1-2 delims=," %%a IN ("%BASES_NAMES%") DO (
ECHO Valeur: %%a
ECHO Valeur: %%b
)


%%a = Base1
%%b = Base2
Last edited by Barty on 11 Jun 2010 05:58, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: String split with separaotr

#3 Post by aGerman » 11 Jun 2010 05:58

Try this

Code: Select all

SET BASES_NAMES=Base1,Base2

FOR /F "tokens=1,2 delims=," %%a IN ("%BASES_NAMES%") DO (
   ECHO Valeur: %%a
   ECHO Valeur: %%b
)


[edit]
Yeah, you're right. The variable name is also wrong.
One more explanation:
- tokens=1,2 is for "first(%%a) and second(%%b) token"
- tokens=1-2 is for "first(%%a) until second(%%b) token"
- tokens=1* is for "first(%%a) token and the whole rest(%%b)"
All these possibilities will work for your example.
[/edit]


Regards
aGerman

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: String split with separaotr

#4 Post by amel27 » 13 Jun 2010 23:07

enumerate all instances:

Code: Select all

SET BASES_NAMES=Base1,Base2

FOR %%a IN (%BASES_NAMES%) DO (
   ECHO Valeur: %%a
)

Post Reply