Prawny
Prawny

Reputation: 13

How to split a number into individual digits in CMD/Batch

I want to split numbers into its individual digits and store them into variables using cmd/batch on Windows. For example, the number '38' should be stored as '3' in VAR1 and '8' in VAR2.

This is my current take on the issue:

@echo off
SET NUMBER=38
FOR /F "TOKENS=1,2 delims=0123456789" %%A IN ("%NUMBER%") DO (CALL :VARSET %%A %%B)
ECHO %NUMBER% has the fallowing digits:
ECHO 1st digit: %VAR1%
ECHO 2nd digit: %VAR2%
PAUSE
EXIT /B 0

:VARSET
set VAR1=%1
set VAR2=%2
GOTO:EOF

I want the output to be:

ECHO 38 has the fallowing digits:
ECHO 1st digit: 3
ECHO 2nd digit: 8

But I get:

ECHO 38 has the fallowing digits:
ECHO 1st digit: Echo is off.
ECHO 2nd digit: Echo is off.

The problem seem to be the fact that I use '0123456789' as my delimiter. Perhaps this is not doable at all using a FOR-LOOP?

Edit: Typos

Upvotes: 1

Views: 1962

Answers (4)

Prawny
Prawny

Reputation: 13

EDIT: Didn't see LotPings answer before I posted mine. Thank you for the answer, works aswell.

@Jeff Zeitlin for the link and aschipfl for the 'lesson' :) A very simply solution is:

@echo off
SET NUMBER=38
SET VAR1=%NUMBER:~0,1%
SET VAR2=%NUMBER:~1,2%
ECHO %NUMBER% has the following digits:
ECHO 1st digit: %VAR1%
ECHO 2nd digit: %VAR2%
PAUSE
EXIT /B 0

Perhaps a more reusable approch would be:

@ECHO OFF
SET COUNT=0
SET NUMBER=1234567890
CALL :SPLITNUM %NUMBER%
PAUSE
EXIT /b 0

:SPLITNUM
SETLOCAL ENABLEDELAYEDEXPANSION
SET NUMBER=%1
:LOOP
SET /A COUNT+=1
SET VAR%COUNT%=%NUMBER:~0,1%
ECHO VAR%COUNT% is !VAR%COUNT%!
SET NUMBER=%NUMBER:~1%
IF NOT "%NUMBER%"=="" (GOTO:LOOP)
ENDLOCAL
GOTO:EOF

Upvotes: 0

Aacini
Aacini

Reputation: 67216

Simpler and faster. Just put the maximum number of digits in the for /L command:

@echo off
setlocal EnableDelayedExpansion

set NUMBER=%1
echo %NUMBER% has the following digits:

for /L %%i in (1,1,12) do if defined NUMBER (
   set "VAR[%%i]=!NUMBER:~0,1!"
   set "NUMBER=!NUMBER:~1!"
)

set VAR[

Upvotes: 2

Compo
Compo

Reputation: 38623

Just for the hell of it:

@Echo Off
SetLocal EnableDelayedExpansion
Set "i=0"
For /F Delims^=^ EOL^= %%A In ('"CMD/U/CEcho %*|Find /V """') Do (Set/A i+=1
    Set "Var[!i!]=%%A")
Set Var[
Timeout -1

Just run it as file.cmd 5678 where file.cmd is the saved name of this script and 5678 an alphanumeric string to split into individual variables.

Upvotes: 0

user6811411
user6811411

Reputation:

Do it with simple math - and I suggest you start with the least significant digit to be able to easily reconstruct the value.

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
:Again
set "#=" & Set Place=0
Set /p "#=Enter # :"
if "%#%" equ "" goto :Eof
Set /A Test=#
:Loop
Set /A "Place+=1,#[!Place!]=Test %% 10,Test/=10"
If "%Test%" gtr "0" goto :Loop
Set #
Echo =====
For /f "delims==" %%A in ('Set #') Do set "%%A="
Goto :Again

Sample run:

Enter # :123456789
#=123456789
#[0]=9
#[1]=8
#[2]=7
#[3]=6
#[4]=5
#[5]=4
#[6]=3
#[7]=2
#[8]=1
=====
Enter # :

Upvotes: 2

Related Questions