[英]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.这是在Windows批处理文件中在第一个字符串中,我有一个数据值列表在第二个字符串中,我有一个变量名称列表我想将第一个字符串中的每个值分配给它在第二个字符串中的相应变量.
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.
这是用户 Aacini 从DosTips.com直接复制的代码。
@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
请注意,这里根本不需要它,但是如果您真的需要将它们设置为看起来更具可读性的变量,您可以,但是一旦您想使用它们(echo 和其他命令),您将需要使用
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!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.