Avatar of pianomanJoe
pianomanJoe
Flag for United States of America asked on

parsing values from string in a windows batch file

Hi.  I am trying to write a windows batch script that parses a string of data to capture key elements.  The string contains both a token and the associated value.  The double carat (^^) separates the value and the double @@ separates the token/value pair.  For example with ACCESSKEY^^$P_ACCESSKEY, ACCESSKEY is the token, 123456 is the value.  @@ indicates a new token/value pair is beginning.  

I need a script that will parse the values from the scripts, i.e. the data between ^^ and @@.  Also, the tokens that begin with APP_PARM_ are optional, so I need to check if they exist, and if they do, parse the full APP_PARM_ token and value.  

Here is the data string that will be evaluated:
ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest

I'm not having any success with generating anything close.  So any help would be greatly appreciated!  Thanks,  Joe
Microsoft DOSWindows OS

Avatar of undefined
Last Comment
Paul Tomasi

8/22/2022 - Mon
Bill Prew

For the sample string, what results do you want returned?

~bp
Bill Prew

Here's a starting point for some parsing in DOS, but not sure exactly how you want to build on this.  Hope it gives you some ideas, let me know what questions or enhancements you want.  The basic idea is to replace the ^^ and @@ with things that we can parse in the FOR statements, and then just work our way through it.

@echo off
set "Data=ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest"
set "Data=%Data:@@=,%"
set "Data=%Data:^^=:%"
for %%Z in (%Data%) do (
  for /F "tokens=1* delims=:" %%A in ("%%~Z") do (
    echo Token=%%~A, Value=%%~B
  )
)

Open in new window

~bp
pianomanJoe

ASKER
Bill,  
Thanks for this.  It works nicely and the display is great!
Is it possible to set variables with a FOR statement?  Ultimately, I need to store the values parsed into a variable so I can reference it again.  I have not be able to set a variable in the DO portion.  I am probably going about it the wrong way.  
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bill Prew

Yes, we can set variables in the FOR body.  What variable names would you want to use, would it be the actual Token name, or something else?

~bp
ASKER CERTIFIED SOLUTION
Bill Prew

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pianomanJoe

ASKER
Bill, I really appreciate your help.  This is working great and I'm learning alot too.  That said, I've some across a problems related to this.  If you can help that would be great!

This is regarding the FOR statement that assign the variables.  I've just been informed that the variables passed in the string may vary by user/application.  The constant is that the token (before the ^^) must start with APP_PARM_.  My example had the tokens I was working with, but another could be APP_PARM_APPNAME or APP_PARM_VOLNAME, or others.   How would I parse the string, identify the tokens as identified in your script(SecretKey,AccessKey, ConfigMethod), but also to identify tokens that start with APP_PARM_ and still use the entire token string for the variable, i.e. APP_PARM_DBUSER as in my example).

Thanks!
Bill Prew

Not sure I exactly understand.  Right now we are parsing the string like:

ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest

Open in new window

to get these variables assigned:

_var_ACCESSKEY=123456
_var_APP_PARM_DBENDPOINT=testdb
_var_APP_PARM_DBNAME=DBTest
_var_APP_PARM_DBPASSWD=access
_var_APP_PARM_DBUSER=portalMaster
_var_CONFIG_METHOD=EBS_CONFIG
_var_SECRETKEY=98654321

Open in new window

we certainly can identify variables that start with "APP_PARM_", but I don't understand what you want to do differently with them?

~bp
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Bill Prew

Here's an example of a change to recognize token names starting with that prefix, and display them, but not sure how you want to handle them differently than the others.

@echo off
setlocal EnableDelayedExpansion

set "Data=ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest"
set "Data=%Data:@@=,%"
set "Data=%Data:^^=:%"

for %%Z in (%Data%) do (
  for /F "tokens=1* delims=:" %%A in ("%%~Z") do (
    set Token=%%~A
    if "!Token:~0,9!" EQU "APP_PARM_" (
      echo Matching name found = [%%~A]
    )
    set "_var_%%~A=%%~B"
  )
)

echo.
echo ***** LIST ALL PARSED VARIABLES AND VALUES *****
set _var

echo.
echo ***** DISPLAY A SINGLE VARIABLES VALUE *****
echo %_var_ACCESSKEY%

endlocal

Open in new window

This produces this output:

Matching name found = [APP_PARM_DBENDPOINT]
Matching name found = [APP_PARM_DBPASSWD]
Matching name found = [APP_PARM_DBUSER]
Matching name found = [APP_PARM_DBNAME]

***** LIST ALL PARSED VARIABLES AND VALUES *****
_var_ACCESSKEY=123456
_var_APP_PARM_DBENDPOINT=testdb
_var_APP_PARM_DBNAME=DBTest
_var_APP_PARM_DBPASSWD=access
_var_APP_PARM_DBUSER=portalMaster
_var_CONFIG_METHOD=EBS_CONFIG
_var_SECRETKEY=98654321

***** DISPLAY A SINGLE VARIABLES VALUE *****
123456

Open in new window

~bp
Paul Tomasi

PLEASE TRY THIS SIMPLE METHOD:


   @echo off
   set params="%~1"
   set params=%params:^==%
   set "params=%params:@@="&& set "%"
   set %params%


AND THAT'S ALL THERE IS TO IT !!


Save this code as TESTARG.BAT for example, and run it specifying your command line parameters:

   testarg ACCESSKEY^^123456@@SECRETKEY^^98654321@@CONFIG_METHOD^^EBS_CONFIG@@APP_PARM_DBENDPOINT^^testdb@@APP_PARM_DBPASSWD^^access@@APP_PARM_DBUSER^^portalMaster@@APP_PARM_DBNAME^^DBTest


You can confirm the variables are set correctly by including the following additional lines of code as shown below:

   @echo off
   set params="%~1"
   set params=%params:^==%
   set "params=%params:@@="&& set "%"
   set %params%

   echo.
   set ACCESSKEY
   set SECRETKEY
   set CONFIG_METHOD
   set APP_PARM_DBENDPOINT
   set APP_PARM_DBPASSWD
   set APP_PARM_DBUSER
   set APP_PARM_DBNAME



(Just fishing for a few points....)
Bill Prew

Interesting treatment Paul.

~bp
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
pianomanJoe

ASKER
Bill,
You're right, nothing needs to be changed.  Any token/value combos added to the string will be parsed.  Guess I just needed a fresh pair of eyes.  Thanks for your help.
Bill Prew

@pianomanJoe

Glad you got something useful, thanks for the interaction, and grade.

~bp
Paul Tomasi

pianomanJoe

I was certain I was going to be awarded SOME points for this one.

The command line parameter format is constant and therefore, my proposed method works for any number of variables.

You say you are learning a lot... Perhaps my method (whose logic may appear too obscure to you) is way too advanced for you even though in reality, it's far simpler to implement than billprew's solution (no disrespect to the old man).

I'm just wondering whether you took the trouble to try my code before totally dismissing it.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pianomanJoe

ASKER
Paul,
Had already decided to use Billprew's suggestion since he was the only contributor until your post.  However, i didn't even see yours when I added my comment and closed the question.  When I did notice it, I tested it.  I'll sure I'll post in the future...
Paul Tomasi

Apologies. It appears to be my fault then for not refreshing my browser's view before posting...