cashonly
cashonly

Reputation: 119

In a batch file, how can I assign a string of values to a list of variables?

This is in a windows batch file In the first string, I have a list of data values In the second string, I have a list of variable names I want to assign the each value in the first string to it's corresponding variable in the second string.

I've tried numerous ways and have been unsuccessful. Below is some code that I've tried that doesn't work. How can I accomplish this?

@echo off
SETLOCAL EnableDelayedExpansion
::SplitStrExample.bat

Set "DataVar=Mike,Smith,123 Main St,Wallawalla,Washington"
Set "VarNames=First Last Address City State"

for %%v in (%VarNames%) do set "%%v=")
FOR /D %%F IN (%DataVar3%) DO (
    for %%v in (%VarNames%) do (
        Set CurVar=%%v
        if "!CurVar!"=="" (
            @echo setting [%%v] to [%%F]
            set %%v=%%F
        )
    )
)

@echo First=%First%
@echo Last=%Last%
@echo Address=%Address%
@echo City=%City%
@echo State=%State%

goto:eof

Upvotes: 1

Views: 3006

Answers (3)

user7818749
user7818749

Reputation:

Please give this one a go :)

@echo off
set "DataVar=Mike,Smith,123 Main St,Wallawalla,Washington"
set "VarNames=First,Last,Address,City,State"

for /f "tokens=1-5 delims=," %%a in ("%VarNames%") do (
   for /f "tokens=1-5 delims=," %%i IN ("%DataVar%") DO (
    echo %%a=%%i
    echo %%b=%%j
    echo %%c=%%k
    echo %%d=%%l
    echo %%e=%%m
  )
)

Carefully notice we are using , as delimeter, therefore the name, surname etc are all only split of from each other by a ,. Default delimeters are whitespace, but here we force a delimeter, therefore whitespace will become part of the value. Note, it is not needed at all here, but if you really needed to set them as variables that looks more readable you could, but then once you want to use them (echo and other commands) you would need to use delayedexpansion

It would then end up something like this:

@echo off
setlocal enabledelayedexpansion
set "DataVar=Mike,Smith,123 Main St,Wallawalla,Washington"
set "VarNames=First,Last,Address,City,State"

for /f "tokens=1-5 delims=," %%a in ("%VarNames%") do (
   for /f "tokens=1-5 delims=," %%i IN ("%DataVar%") DO (
    set %%a=%%i
    set %%b=%%j
    set %%c=%%k
    set %%d=%%l
    set %%e=%%m
  )
  :# you can echo each value, without having to know the variable name
  echo !%%a! !%%b! !%%c! !%%d! !%%e!
  :# or echo them as know variable names
  echo !First! !Last! !Address! !City! !State!
)

Upvotes: 2

Aacini
Aacini

Reputation: 67216

Ok. A more classical approach...

@echo off
setlocal EnableDelayedExpansion

Set "DataVar=Mike,Smith,123 Main St,Wallawalla,Washington"
Set "VarNames=First Last Address City State"

for %%v in (%VarNames%) do for /F "tokens=1* delims=," %%a in ("!DataVar!") do set "%%v=%%a" & set "DataVar=%%b"

for %%v in (%varNames%) do echo %%v=!%%v!

Upvotes: 2

Squashman
Squashman

Reputation: 14290

I literally cannot take any credit for this answer. This is a direct copy of code from DosTips.com by user Aacini.

@echo off
SETLOCAL EnableDelayedExpansion

Set "DataVar=Mike,Smith,123 Main St,Wallawalla,Washington"
Set "VarNames=First Last Address City State"

set "p=%%"
set "v=%VarNames: =" & set "s=!DataVar:*,=!" & call set "!v!=!p!DataVar:,!s!=!p!" & set "DataVar=!s!" & set "v=%" & set "!v!=!s!"

echo First=%First%
echo Last=%Last%
echo Address=%Address%
echo City=%City%
echo State=%State%

Upvotes: 4

Related Questions