Jaggar H
Jaggar H

Reputation: 13

Set multiple variables from 1 input

The user puts in one input (2 words, space as delimiter), and that is set to 2 variables.

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

That of course won't work, but that's all I know how to do.

Upvotes: 0

Views: 2528

Answers (2)

Squashman
Squashman

Reputation: 14290

A little trickery using string substitution with the SET command. This will allow multiple space delimited strings to be entered and it will number them up accordingly.

@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

Upvotes: 0

Stephan
Stephan

Reputation: 56155

use a for loop to split the string:

@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

Note: this may fail with some special characters in the input string.

Upvotes: 1

Related Questions