0

So i was making a batch file recently, and i made a string. Later i needed to set each number as its own string (its hard to describe).

For example:

the input is:

set str=2043

so, i want to set 2, 0, 4 and 3 as their own string like this:

set str1=2
set str2=0
set str3=4
set str4=3

but lets say i dont know what str is. i just know that its 4 numbers.

I tried to browse the web but the i didnt find anything :P

Thanks for the help :D

  • No, I don't understand. Please, post a clear example, like "if input is `set string=2043` the output is..." Please, modify the question, do NOT post details or code in comments! – Aacini May 08 '15 at 20:52
  • sorry, im kinda new to this website. but anyway i just edited it now, hope its more clear now :) tell me if its not –  May 08 '15 at 21:02
  • Not yet. Given `set str=2043` as input (and the other values in the example), what is the OUTPUT you want??? Or, are the `str1=2` etc. the output? – Aacini May 08 '15 at 21:04
  • yeah thats the output! :) –  May 08 '15 at 21:07

1 Answers1

0

The name of the data you want is array:

@echo off
setlocal EnableDelayedExpansion

set string=2043

rem Separate each digit in its "own string" (element) of "str" array
set i=0
for /F "delims=" %%a in ('cmd /U /C echo %string%^| find /V ""') do (
   set /A i+=1
   set "str!i!=%%a"
)

set str

Previous code separate the string "2043" in an array called "str" with 4 elements. For further details on array management in Batch files, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108