Windows Batch: Split String to individual characters to variables
Trying to do this but in a function of its own.
I have a 4 digit number that I am trying to split into distinct variables but If there is a way to do it so that it would work for someone who doesn't know how many variables they are using I would prefer that. The following is my code so far.
REM @echo off
setlocal
:start
set /p nStore="Enter 4 digit store number:"
call :split nStore n1 n2 n3 n4
:pass2
echo %result%
echo %n1%
goto :eof
:split <nStore> <n1> <n2> <n3> <n4>
(
setlocal EnableDelayedExpansion
set "tmpStore=!%~1!"
set "count=0"
:loop
if defined tmpStore
(
set tmpStore=%tmpStore:~1%
set /a count+=1
set /a pos=%count%-1
set n!count!=!str:~%pos%,1!
goto loop
)
endlocal
goto :pass2
)
When I call the :split I keep getting an error "The Syntax of the command is incorrect". Right now I am trying to get :split to store my 4 digit "store number" into variables. So if my store number is 9876 I expect n1 = 9 n2 = 8 n3 = 7 n4 = 6 as variables. Question being what is wrong with my code that it errors out.