[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3697
  • Last Modified:

Parsing Delimited String in BAT Script

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
revof11_
Asked:
revof11_
  • 5
  • 4
1 Solution
 
Steve KnightIT ConsultancyCommented:
Use for /d instead and comma delimited:

set MYSTRING=Tom,Dick,Harry
 
for /D %%i in (%MYSTRING%) do (
  echo %%i Something
)
0
 
Steve KnightIT ConsultancyCommented:
In your example the other two options get assigned to %%j and %%k
0
 
revof11_Author Commented:
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
revof11_Author Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
revof11_Author Commented:
Let me try that.
Replacing the delim is acceptable for the solution.
0
 
Steve KnightIT ConsultancyCommented:
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
 
revof11_Author Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
AmazingTechCommented:
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

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.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now