Solved

Parsing Delimited String in BAT Script

Posted on 2008-10-15
10
3,646 Views
Last Modified: 2012-05-05
I am attempting to parse a string that contains a set of delimited strings.
I want to perform an action on each string within this.

I just can't get it working.
The following code snippets just givs me either the whole line or just the first thing (like the following).

Tom Something
Tom Dick Harry Something

What I **WANT** is output like this:

Tom Something
Dick Something
Harry Something

In the shell script version of this I was able to accomplish it no problem.
Wasn't so bad... but this is just frustrating me to no extent.
set MYSTRING=Tom;Dick;Harry
 

for /f "tokens=1,* delims=;" %%i in ("%MYSTRING%") do (

  echo %%i Something

)

Open in new window

0
Comment
Question by:revof11_
  • 5
  • 4
10 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22725042
Use for /d instead and comma delimited:

set MYSTRING=Tom,Dick,Harry
 
for /D %%i in (%MYSTRING%) do (
  echo %%i Something
)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22725056
In your example the other two options get assigned to %%j and %%k
0
 

Author Comment

by:revof11_
ID: 22725250
I don't have a choice as to the delimiter, nor the size of the string that actually gets parsed.  Using the following code snippet doesn't work, either.
for /D %%i in ("%MYSTRING%") do (

  echo %%i Something

)

Open in new window

0
 

Author Comment

by:revof11_
ID: 22725296
sorry... i meant to attach this code snippet
set MYSTRING=Tom;Dick;Harry;Harriet

for /D "delims=;" %%i in ("%MYSTRING%") do (

  echo %%i Something

)

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22725378
It works only with commas.  Any chance you can change to commas with a replace line:

set MYSTRING=Tom;Dick;Harry;Harriet
set mystring=%mystring:;=,%
for /D %%i in (%MYSTRING%) do (
  echo %%i Something
)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:revof11_
ID: 22725406
Let me try that.
Replacing the delim is acceptable for the solution.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22725408
Don't know where the /D came into my earlier example, don;t need it :

set MYSTRING="Tom jones";"Dick someone";Harry;Harriet
set mystring=%mystring:;=,%
for %%i in (%MYSTRING%) do (
  echo %%~i Something
)

Very difficult to type anything at the moment as EE is only giving a one line window to type a reponse in for some reason ...
0
 

Author Comment

by:revof11_
ID: 22725444
Attached worked perfectly.
Thanks!   :)
set MYSTRING=Tom;Dick;Harry;Harriet

set TEMPSTR=%MYSTRING:;=,%
 

for /D %%i in (%TEMPSTR%) do (

  echo %%i Something

)

Open in new window

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 50 total points
ID: 22725499
No problem.... if you have spaces in be careful and use quotes around each item and the extra ~ to remove them again as in ID:22725408 above.

Don't forget to select an answer... and you might want to consider giving more than 50 points in the future too, hadn't noticed the low value.

Steve
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22734827
Did this not work?
set MYSTRING=Tom;Dick;Harry;Harriet
 

for %%i in (%MYSTRING%) do (

  echo %%i Something

)

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now