?
Solved

Parsing Delimited String in BAT Script

Posted on 2008-10-15
10
Medium Priority
?
3,676 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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