简体   繁体   中英

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

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%

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!
)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM