Batch Split String to individual variables -
windows batch: split string individual characters variables
trying in function of own.
i have 4 digit number trying split distinct variables if there way work doesn't know how many variables using prefer that. following code 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 call :split keep getting error "the syntax of command incorrect". right trying :split store 4 digit "store number" variables. if store number 9876 expect n1 = 9 n2 = 8 n3 = 7 n4 = 6 variables. question being wrong code errors out.
@echo off setlocal rem @echo off setlocal :start set /p nstore="enter 4 digit store number:" call :split nstore n1 n2 n3 n4 :pass2 echo %result% echo %nstore% %n1% %n2% %n3% %n4% goto :eof :split <nstore> <n1> <n2> <n3> <n4> setlocal enabledelayedexpansion set "n4=!%~1!" endlocal&set "n4=%n4%" set "n1=%n4:~0,1%" set "n2=%n4:~1,1%" set "n3=%n4:~2,1%" set "n4=%n4:~3,1%" goto :eof you don't need send destination parameter names subprocedure. send %nstore% instead of nstore avoid needing invoke delayed expansion
...actually, could avoid delayedexpansion using
for /f "tokens=1,2delims==" %%a in (`set %1`) if /i "%%a"=="%1" set "n4=%%b" if using delayedexpansion, need endlocal... make value of n4 available outside of setlocal/endlocal bracket.
theremainder of code self-evident. aren't setting result, that echo resolve echo hence provide echo-status report. i've changed second echo provide list of of variables in question.
and of course there's no checking done input data of length 4 or of only-numerics.
Comments
Post a Comment