Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parsing Delimited String in BAT Script

Posted on 2008-10-15
10
Medium Priority
?
3,689 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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
 

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 200 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

609 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