Forums before death by AOL, social media and spammers... "We can't have nice things"
|    alt.msdos.batch.nt    |    Fun with Windows NT batch files    |    68,980 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 67,782 of 68,980    |
|    Zaidy036 to Robert Prins    |
|    Re: Get String Length    |
|    03 Sep 21 17:23:13    |
      From: Zaidy036@air.isp.spam              On 9/3/2021 8:46 AM, Robert Prins wrote:       > On 2021-09-01 19:30, Tom Del Rosso wrote:       >> Here's a subroutine to get the length of a string that uses successive       >> approximation so it runs very fast because it only loops 13 times (once       >> for each bit of the resulting number) no matter how long the string is.       >>       >>       >> :GetStringLength       >> rem Takes string variable NAME which must have no spaces. Returns       >> variable: StringLength       >> rem Environment variables go up to 8191 characters so approximation       >> starts with 4096 which is the MSB.       >> rem Practical limit is 8185 because "set x=" has 6 characters       >> if [%1]==[] set "StringLength=" & goto :eof       >> if not defined %1 set /a StringLength=0 & goto :eof       >> set /a GetStringLength_PlaceValue=4096       >> set /a GetStringLength_Guess=0       >> :GetStringLength_Loop       >> set /a GetStringLength_Guess^|=GetStringLength_PlaceValue       >> set /a GetStringLength_Skip=GetStringLength_Guess-1       >> call set "GetStringLength_Char=%%%1:~%GetStringLength_Skip%,1%%"       >> if not defined GetStringLength_Char set /a       >> GetStringLength_Guess^^=GetStringLength_PlaceValue       >> set /a GetStringLength_PlaceValue/=2       >> if %GetStringLength_PlaceValue% GTR 0 goto :GetStringLength_Loop       >> set /a StringLength=GetStringLength_Guess       >> set "GetStringLength_String="       >> set "GetStringLength_PlaceValue="       >> set "GetStringLength_Guess="       >> set "GetStringLength_Skip="       >> set "GetStringLength_Char="       >> goto :eof       >       >       > We're living in 2021, why write convoluted code when there are much       > better languages to do this? REXX (available for about every OS) can do       > it in one line, and if you only use Windoze, PowerShell would probably       > do it.       >       > Robert              another batch:              :: _StrngLen.bat              :: _StrngLen.bat "string" Length at exit       :: _StrngLen.bat "string" _Len Variable for length       :: https://ss64.com/nt/syntax-strlen.html              @ECHO OFF       ECHO(       FOR %%F IN (%1 %2 %3 %4 %5 %6 %7 %8 %9) DO CALL :LnCalc %%F              :EOF              :LnCalc       Setlocal EnableDelayedExpansion       :: strLen String [RtnVar]       :: -- String The string to be measured, surround in quotes       if it contains spaces.       :: -- RtnVar An optional variable to be used to return the       string length.       ECHO(       SET "s=#%~1"       SET "len=0"       For %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (        if "!s:~%%N,1!" neq "" (        SET /a "len+=%%N"        SET "s=!s:~%%N!"        )       )       :: Endlocal & IF "%~2" neq "" (SET %~2=%len%) else ECHO %len% of %1       Endlocal & ECHO %len% of %1       GOTO :EOF              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca