Solved

Parsing Delimited String in BAT Script

Posted on 2008-10-15
10
3,639 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
Comment Utility
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
Comment Utility
In your example the other two options get assigned to %%j and %%k
0
 

Author Comment

by:revof11_
Comment Utility
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_
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Expert Comment

by:Steve Knight
Comment Utility
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_
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now