Solved

Parsing Delimited String in BAT Script

Posted on 2008-10-15
10
3,651 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
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: 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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