Parsing a Variable into multiple Variables

T

TJT

This is a follow-up to a previous post regarding passing variables that
could contain embedded spaces. Normally this wouldn't be a problem because
I could just enclose a value containing a space within double-quotes.
Because of some problems I am having with the START command - I am having
problems passing in parameters with Double-quotes around it

Let's say we have the following...
SET TestVar=Val1, Val 2 with spaces, Val3

I would like to be able to parse the %TestVar% value into 3 variables using
the comma as a Delimiter. The name of the variables isn't important but
let's say I want them called P1, P2 and P3. I would basically want to parse
it as follows:
P1=Val1
P2=Val 2 with spaces
P3=Val3

I would think that something like this is doable with the For command but I
can not figure it out.

Thanks in advance,
--Tom
I have a variable %
 
P

Phil Robyn [MVP]

TJT said:
This is a follow-up to a previous post regarding passing variables that
could contain embedded spaces. Normally this wouldn't be a problem because
I could just enclose a value containing a space within double-quotes.
Because of some problems I am having with the START command - I am having
problems passing in parameters with Double-quotes around it

Let's say we have the following...
SET TestVar=Val1, Val 2 with spaces, Val3

I would like to be able to parse the %TestVar% value into 3 variables using
the comma as a Delimiter. The name of the variables isn't important but
let's say I want them called P1, P2 and P3. I would basically want to parse
it as follows:
P1=Val1
P2=Val 2 with spaces
P3=Val3

I would think that something like this is doable with the For command but I
can not figure it out.

Thanks in advance,
--Tom
I have a variable %

C:\cmd>demo\ParseThreeVariables
_V1=Val1
_V2=Val 2 with spaces
_V3=Val3

C:\cmd>rlist demo\ParseThreeVariables.cmd
=====begin C:\cmd\demo\ParseThreeVariables.cmd ====================
1. @echo off
2. setlocal
3. SET TestVar=Val1, Val 2 with spaces, Val3
4. for /f "tokens=1-3 delims=," %%a in (
5. "%TestVar%"
6. ) do set _V1=%%a&set _V2=%%b&set _V3=%%c
7. for /l %%a in (2,1,3) do set _V%%a=!_V%%a:~1!
8. set _V
=====end C:\cmd\demo\ParseThreeVariables.cmd ====================
 
G

Garry Deane

This is a follow-up to a previous post regarding passing variables that
could contain embedded spaces. Normally this wouldn't be a problem because
I could just enclose a value containing a space within double-quotes.
Because of some problems I am having with the START command - I am having
problems passing in parameters with Double-quotes around it

Let's say we have the following...
SET TestVar=Val1, Val 2 with spaces, Val3

I would like to be able to parse the %TestVar% value into 3 variables using
the comma as a Delimiter. The name of the variables isn't important but
let's say I want them called P1, P2 and P3. I would basically want to parse
it as follows:
P1=Val1
P2=Val 2 with spaces
P3=Val3

I would think that something like this is doable with the For command but I
can not figure it out.

Thanks in advance,
--Tom
I have a variable %

Rather than passing a single variable with all the arguments, it would
be simpler to pass the names of the variables:

@echo off
setlocal
set P1=Val1
set P2=Val 2 with spaces
set P3=Val3
call :sub P1 P2 P3
goto :eof

:sub
setlocal enabledelayedexpansion
set Param1=!%1!
set Param2=!%2!
set Param3=!%3!
set param
endlocal
goto :eof

Alternately, do you actually need to pass the variables as parameters.
If you are invoking another batch file with the start command, it will
receive the parent's environment including any variables that the
parent has defined.

:: Parent.bat
@echo off
setlocal
set P1=Val1
set P2=Val 2 with spaces
set P3=Val3
start "test" "d:\test folder\child.bat"
goto :eof

:: d:\test folder\child.bat
@echo off
echo [%p1%] [%p2%] [%p3%]
pause
exit

Garry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top