Make variable automatically count inside FOR loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Q. April
Posts: 19
Joined: 27 Jan 2018 14:36
Location: Mayatopia

Make variable automatically count inside FOR loop

#1 Post by Q. April » 15 Feb 2018 17:40

Sample code manually set Variable n1 to n12, but when string have more than 12 digits ,it really tired to manually enter new SET Command

Code: Select all

set fn=956878543546

set str=%fn%

set n1=%str:~-1,1%
set n2=%str:~-2,1%
set n3=%str:~-3,1%
set n4=%str:~-4,1%
set n5=%str:~-5,1%
set n6=%str:~-6,1%
set n7=%str:~-7,1%
set n8=%str:~-8,1%
set n9=%str:~-9,1%
set n10=%str:~-10,1%
set n11=%str:~-11,1%
set n12=%str:~-12,1%

how to make variable(%n1%) automatically count to (%n2%) And count (%str:~-1,1% ) to (%str:~-2,1% )inside FOR loop,

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Make variable automatically count inside FOR loop

#2 Post by Squashman » 15 Feb 2018 21:10

I think this is what you want to do but not sure. This makes it a bit more dynamic as it gets the length of the string.

Code: Select all

@echo on
SETLOCAL enabledelayedexpansion
set "fn=956878543546"
set "str=%fn%"
CALL :strLen str length

set "total=0"

FOR /L %%G IN (%length%,-1,1) DO set /a total+=!str:~-%%G,1!

echo %total%
pause
GOTO :EOF

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source https://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

Q. April
Posts: 19
Joined: 27 Jan 2018 14:36
Location: Mayatopia

Re: Make variable automatically count inside FOR loop

#3 Post by Q. April » 16 Feb 2018 11:51

Thank Squashman,i try it

Q. April
Posts: 19
Joined: 27 Jan 2018 14:36
Location: Mayatopia

Re: Make variable automatically count inside FOR loop

#4 Post by Q. April » 17 Feb 2018 11:17

Thank again Squashman ,you give me idea, after modified that,i got 100% what i need...

Code: Select all

@echo off
SETLOCAL enabledelayedexpansion
set "fn=987654321"
set "str=%fn%"
CALL :strLen str length
set "n=0"

FOR /L %%G IN (1,1,%length%) DO (
    set /a n=!str:~-%%G,1!
    set n%%G=!n!
    )

echo %Str%
echo %n1% %n2% %n3% %n4% %n5% %n6% %n7% %n8% %n9%  test variable increment
pause
GOTO :EOF

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source https://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Make variable automatically count inside FOR loop

#5 Post by Squashman » 17 Feb 2018 16:03

You don't need the extra set command. You can just use:

Code: Select all

set "n%%G=!str:~-%%G,1!"

Q. April
Posts: 19
Joined: 27 Jan 2018 14:36
Location: Mayatopia

Re: Make variable automatically count inside FOR loop

#6 Post by Q. April » 19 Feb 2018 08:31

Thanks........

Post Reply