从1个输入设置多个变量

时间:2018-02-01 15:02:15

标签: batch-file variables cmd

用户输入一个输入(2个字,空格作为分隔符),并设置为2个变量。

@echo off
set /p input=Input:
set var1=%input%
set var2=%input2%
echo %var1%
echo %var2%
pause

那当然不会奏效,但这就是我所知道的。

2 个答案:

答案 0 :(得分:1)

使用for循环来分割字符串:

@echo off
setlocal enabledelayedexpansion
set /p "input=Input: "
set count=0
for %%a in (%input%) do (
  set /a count+=1
  set var!count!=%%a
)
echo found %count% words:
set var

注意:输入字符串中的某些特殊字符可能会失败。

答案 1 :(得分:0)

使用SET命令使用字符串替换的一点点技巧。这将允许输入多个以空格分隔的字符串,并相应地对它们进行编号。

@echo off
setlocal EnableDelayedExpansion
set /p "input=Input: "
set i=1
set "var!i!=%input: =" & set /A i+=1 & set "var!i!=%"
set var
pause