strLen script question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

strLen script question

#1 Post by Jer » 18 Jan 2018 00:20

Thanks for your help with strLen. I get "0" length.
After the line with set "str=A!%~1!" the value of !str! is A
A look at %* in strLen shows: "test" len

Code: Select all

@echo off
setlocal EnableDelayedExpansion
call :strLen "test" len
echo length is %len%
endlocal & exit /b

: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
C:\Temp\DOSBatch>test
length is 0

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: strLen script question

#2 Post by aGerman » 18 Jan 2018 01:16

As the description states
-- string [in] - variable name containing the string being measured for length
You have to pass only the name of a variable that contains your string

Code: Select all

set "var=test"
call :strLen var len
Steffen

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: strLen script question

#3 Post by Jer » 18 Jan 2018 11:34

Variable name is required by strLen--not the value.
I wish I had read the description :oops:
Thanks Steffen.

Post Reply