简体   繁体   English

在批处理文件中,如何将一串值分配给变量列表?

[英]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.

相关问题 如何在具有一系列变量的批处理文件中循环执行一组命令? - How can I loop a set of commands in a batch file with a series of variables? 如何在Windows批处理的for循环中从文本文件分配变量 - How to assign variables from text file in for loop of windows batch 如何用带引号的字符串替换Windows批处理文件中的%% f? - How can I replace %%f in a Windows batch file with a quoted string? 如何在批处理文件中的两个引号之间获取字符串? - How can I get a string between two quotes in a batch file? 如何使用批处理脚本从文本文件创建用户并分配组? - How can I create users and assign group from a text file with a batch script? 我可以在哪些 C++ 编译器中为结构变量赋值? - In which C++ Compilers I can assign values to struct variables? 有什么方法可以在批处理程序中创建带有“逗号”的字符串变量 - Is there any way I can create a string variables with "commas" in batch programme 如何在输出重定向中使用vbscript变量从批处理脚本写入配置文件 - How can I write a config file from batch script using vbscript variables in output redirect 如何使用从 txt 文件中读取的变量使用批处理来输出文本块? - How can I use batch to output a block of text using variables read in from a txt file? 如何在python中获取文件名列表并将每个文件名分配为数字以供以后使用? - How can I take a list of file names in python and assign each file name as a number for later use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM