Discussion:
Get String Length
(too old to reply)
Tom Del Rosso
2021-09-01 19:30:18 UTC
Permalink
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
Tom Del Rosso
2021-09-01 19:39:20 UTC
Permalink
Oops the REM lines wrapped and one other line did:

if not defined GetStringLength_Char set /a
GetStringLength_Guess^^=GetStringLength_PlaceValue
JJ
2021-09-02 04:13:19 UTC
Permalink
Post by Tom Del Rosso
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.
[snip]

Nice. Would definitely be part of by batch subs library.

The method could be used as a fast `HasStr` sub. :)
Tom Del Rosso
2021-09-02 07:08:01 UTC
Permalink
Post by JJ
Post by Tom Del Rosso
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. [snip]
Nice. Would definitely be part of by batch subs library.
The method could be used as a fast `HasStr` sub. :)
What does HasStr do? Find a substring?
--
Defund the Thought Police
Harry Potter
2021-09-02 19:49:20 UTC
Permalink
JJ, I have a batch file that backs up a user program without rebuildable files to a specified folder using 7Zip and one that returns the current date. The former is hard-coded for my needs but can be customized by other downloaders, and the latter simply returns the current date in the environment variable %DATE2%. The former first asks for the subfolder (I call it the system, as that is how I organize my backups, but you can change the prompt text.) then makes a temporary copy of the program, deletes rebuildable files (mainly programs and object files), compresses the folder, attaches the date to the name, moves the file to the destination and then deletes the temporary folder. Are you interested? If so, I ask for a way to send you the files in any way you like. I have some information on sourceforge.net and can upload them there, or I can e-mail you the files.

BTW, I have other batch files that I use. I'll look at them now.
Harry Potter
2021-09-02 20:11:40 UTC
Permalink
JJ, I have a batch file that backs up a user program without rebuildable files to a specified folder using 7Zip and one that returns the current date. The former is hard-coded for my needs but can be customized by other downloaders, and the latter simply returns the current date in the environment variable %DATE2%. The former first asks for the subfolder (I call it the system, as that is how I organize my backups, but you can change the prompt text.) then makes a temporary copy of the program, deletes rebuildable files (mainly programs and object files), compresses the folder, attaches the date to the name, moves the file to the destination and then deletes the temporary folder. Are you interested? If so, I ask for a way to send you the files in any way you like. I have some information on sourceforge.net and can upload them there, or I can e-mail you the files.
BTW, I have other batch files that I use. I'll look at them now.
I looked at the batch files. They are mostly for my work with emulation, but I have some batch files to compress folders using 7Zip or WInMount to specific locations and assemble a code file to a .COM file using NASM. Is anybody interested?
Harry Potter
2021-09-02 20:18:50 UTC
Permalink
I also have two batch files to compress executables using UPX, one to compress files to .gz files using 7Zip and one that automatically decompresses archives to a specific folder.
Harry Potter
2021-09-02 20:31:51 UTC
Permalink
The easiest way to use them is to add them to the SendTo folder using SendTo Toys. You can find it on freewarefiles.com.
JJ
2021-09-03 05:11:24 UTC
Permalink
Post by Tom Del Rosso
Post by JJ
Post by Tom Del Rosso
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. [snip]
Nice. Would definitely be part of by batch subs library.
The method could be used as a fast `HasStr` sub. :)
What does HasStr do? Find a substring?
Yes. A sub to check the existence of a substring when the index of the
substring doesn't matter. It's basically just a sub which finds the index of
a substring.
JJ
2021-09-04 09:46:50 UTC
Permalink
Post by JJ
Post by Tom Del Rosso
What does HasStr do? Find a substring?
Yes. A sub to check the existence of a substring when the index of the
substring doesn't matter. It's basically just a sub which finds the
index of a substring.
So you're thinking of doing it this way?
[snip]

Huh? Why are you using string lengths to check the existence of a substring?
Tom Del Rosso
2021-09-05 16:53:16 UTC
Permalink
Post by JJ
Post by JJ
Post by Tom Del Rosso
What does HasStr do? Find a substring?
Yes. A sub to check the existence of a substring when the index of
the substring doesn't matter. It's basically just a sub which finds
the index of a substring.
So you're thinking of doing it this way?
[snip]
Huh? Why are you using string lengths to check the existence of a substring?
I don't know! I thought that was what you were suggesting when you said:

"The method could be used as a fast `HasStr` sub. :)"
JJ
2021-09-06 07:07:40 UTC
Permalink
Post by Tom Del Rosso
Post by JJ
Post by JJ
Post by Tom Del Rosso
What does HasStr do? Find a substring?
Yes. A sub to check the existence of a substring when the index of
the substring doesn't matter. It's basically just a sub which finds
the index of a substring.
So you're thinking of doing it this way?
[snip]
Huh? Why are you using string lengths to check the existence of a substring?
"The method could be used as a fast `HasStr` sub. :)"
By "method", I meant the algorithm. Not the sub itself as is.

But nevermind though, cause I found out that the divide-and-conquer
algorithm can't be used as a faster substring search than sequential one.
Tom Del Rosso
2021-09-06 16:45:46 UTC
Permalink
Post by JJ
Post by Tom Del Rosso
Post by JJ
Post by JJ
Post by Tom Del Rosso
What does HasStr do? Find a substring?
Yes. A sub to check the existence of a substring when the index of
the substring doesn't matter. It's basically just a sub which
finds the index of a substring.
So you're thinking of doing it this way?
[snip]
Huh? Why are you using string lengths to check the existence of a substring?
"The method could be used as a fast `HasStr` sub. :)"
By "method", I meant the algorithm. Not the sub itself as is.
But nevermind though, cause I found out that the divide-and-conquer
algorithm can't be used as a faster substring search than sequential one.
If the objective is to find exactly where the substring exists then it
could actually be faster for a long string.

I mean using the asterisk in string substitution like below, and testing
the length of the result. Even more so if you already know the length of
the original string.

call set poststring=%%string:*%substring%=%%
JJ
2021-09-07 11:24:17 UTC
Permalink
Post by Tom Del Rosso
Post by JJ
Post by Tom Del Rosso
Post by JJ
Post by JJ
Post by Tom Del Rosso
What does HasStr do? Find a substring?
Yes. A sub to check the existence of a substring when the index of
the substring doesn't matter. It's basically just a sub which
finds the index of a substring.
So you're thinking of doing it this way?
[snip]
Huh? Why are you using string lengths to check the existence of a substring?
"The method could be used as a fast `HasStr` sub. :)"
By "method", I meant the algorithm. Not the sub itself as is.
But nevermind though, cause I found out that the divide-and-conquer
algorithm can't be used as a faster substring search than sequential one.
If the objective is to find exactly where the substring exists then it
could actually be faster for a long string.
I mean using the asterisk in string substitution like below, and testing
the length of the result. Even more so if you already know the length of
the original string.
call set poststring=%%string:*%substring%=%%
Oh, I see. I didn't realize asterisk as a leading wildcard is supported for
string replacement. That's indeed a faster way to find the substring offset.
Thanks.

Though, in a different topic but related to that asterisk wildcard...
Although I haven't yet need to do it, is it possible to replace literal
asterisk character itself excluding any characters preceeding it, using
replace string substitution? I've tried escaping the asterisk and using
double asterisks, but it doesn't work. For example, to replace `*` with `;`,
so that e.g. `abc*def` would become `abc;def`.

@echo off
setlocal
call :repesc "abc*def" result
echo escape=%result%
call :repdbl "abc*def" result
echo double=%result%
goto :eof

:repesc
set "a=%~1"
set "%2=%a:^*=;%"
goto :eof

:repdbl
set "a=%~1"
set "%2=%a:**=;%"
goto :eof

Here, the result are:

escape=abc*def
double=;def
Tom Del Rosso
2021-09-07 21:10:36 UTC
Permalink
Post by JJ
Oh, I see. I didn't realize asterisk as a leading wildcard is
supported for string replacement. That's indeed a faster way to find
the substring offset. Thanks.
Though, in a different topic but related to that asterisk wildcard...
Although I haven't yet need to do it, is it possible to replace
literal asterisk character itself excluding any characters preceeding
it, using replace string substitution? I've tried escaping the
asterisk and using double asterisks, but it doesn't work. For
example, to replace `*` with `;`, so that e.g. `abc*def` would become
`abc;def`.
@echo off
setlocal
call :repesc "abc*def" result
echo escape=%result%
call :repdbl "abc*def" result
echo double=%result%
goto :eof
:repesc
set "a=%~1"
set "%2=%a:^*=;%"
goto :eof
:repdbl
set "a=%~1"
set "%2=%a:**=;%"
goto :eof
escape=abc*def
double=;def
I guess you could use ** to chop off the first part of the string, then
measure its length and then reassemble it, like

set x=abc*def
set y=%x:**=;%
[...get length of x and y and x-y...]
set /a prefix_len = x_len - y_len
call set z=%%x:~0,%prefix_len%%%%y%
set z
pause


But my first idea started in the wrong direction.
I thought of putting something before the asterisk like

set y=%x:c*=c;%

which would work if you know the letter before it is c.

To try every letter you have to use FIND to do the substitution at the
right time (or else it would change back when it got to the next
letter), like this, but it's much slower.

@echo off
set y=abc*def
set "z="
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
echo %y% | find "%%a*" >nul
if not errorlevel 1 (
call set z=%%y:%%a*=%%a$%%
)
)
set y
set z
pause
--
Defund the Thought Police
Tom Del Rosso
2021-09-02 06:55:32 UTC
Permalink
I just noticed that there is a vestigial line still in there.

Remove this:

set "GetStringLength_String="
Robert Prins
2021-09-03 12:46:21 UTC
Permalink
Post by Tom Del Rosso
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
--
Robert AH Prins
robert(a)prino(d)org
The hitchhiking grandfather - https://prino.neocities.org/indez.html
Some REXX code for use on z/OS - https://prino.neocities.org/zOS/zOS-Tools.html
Kenny McCormack
2021-09-03 20:20:47 UTC
Permalink
In article <sgsqun$h78$***@dont-email.me>,
Robert Prins <***@prino.org> wrote:
...
Post by Robert Prins
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.
Because those are the rules of this newsgroup. Like several other Usenet
newsgroups, this group is kind of an abstraction. I.e., the issue isn't
solving specific problems by whatever means work - i.e., if it works, it is
good mentality. Rather, it is solving the problem using only a specific
set of tools - sort of like "straight edge and compass" problems in
geometry.

The rules - which, BTW, I didn't make up, and I, of course, have no
authority to enforce, but which I have gleaned from years of observing this
group - is that you can only solve problems using functionality found in
CMD.EXE itself (and COMMAND.COM before then). You are not allowed to use
any external tools or languages. To do so would be cheating.

Note, BTW, that you are sort of allowed (wink, wink, nudge, nudge) to cheat
semi-legally by using things like WishScript, or JavaScript, or
WhateverScript or PowerShell, because you can sort of make a case that
these things are now builtin on most modern Windows installs. But the
point is that most people still using DOS/Windows (without any external
tools) are probably doing so on older machines (I still have several XP
machines), and those tools won't be present on those older machines.

In fact, many of the people still using CMD.EXE batch language, are
probably running their PCs by oil light, in a cave somewhere in Afghanistan
(or similar).

BTW, REXX? Seriously???
--
"Only a genius could lose a billion dollars running a casino."
"You know what they say: the house always loses."
"When life gives you lemons, don't pay taxes."
"Grab 'em by the p***y!"
Zaidy036
2021-09-03 21:23:13 UTC
Permalink
Post by Robert Prins
Post by Tom Del Rosso
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
mokomoji
2021-09-07 16:34:33 UTC
Permalink
Post by Zaidy036
Post by Robert Prins
Post by Tom Del Rosso
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
:: _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
@echo off
setlocal
Set "_demo=some example string don with enabledelayedexpansion version"
Call :strlen "_length" "%_demo%"
Echo String is %_length% characters long
goto :end

:strlen
set "s=#%~2"
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
for /f "delims=" %%f in ('echo "%%s:~%%N,1%%"') do (
if "%%~f" neq "" (
call set /a "len+=%%N"
call set "s=%%s:~%%N%%"
rem call echo %%f %%N %%s%%
)))
if "%~1" neq "" (set %~1=%len%) else (echo %~1 = %%%~1%%)
goto :eof

:end
endlocal
pause


god them..~!!
Tom Del Rosso
2021-09-04 02:20:16 UTC
Permalink
Post by Robert Prins
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.
You can also do it with one line in a batch file like so:

call :GetStringLength

Everyone here knows other languages. We use batch when it's the best
choice.

If it's never the best choice for you, then you wouldn't have come here.

BTW REXX was included in OS/2 in 1993. I was an advocate. No one
listened. Since you emphasize that it's 2021 you probably should have
suggested Python.

And also, successive approximation isn't convoluted. It has a lot of
applications. There are successive approximation register chips that do
it in hardware for A/D conversion. My method was IMO very
straightforward and readable, and I'm a fan of Knuth's law, "A program
should be readable by humans first and computers second."
Zaidy036
2021-09-03 22:00:12 UTC
Permalink
Post by Tom Del Rosso
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
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
Zaidy036
2021-09-04 15:56:21 UTC
Permalink
Post by Zaidy036
Post by Zaidy036
Post by Zaidy036
_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
Post by Zaidy036
EOF
LnCalc
Setlocal EnableDelayedExpansion
Post by Zaidy036
Post by Zaidy036
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!"
)
)
Post by Zaidy036
Post by Zaidy036
Endlocal & IF "%~2" neq "" (SET %~2=%len%) else ECHO %len% of %1
Endlocal & ECHO %len% of %1
GOTO :EOF
Another way to do successive approximation. And it's probably faster.
Did you just write that?
at the top <https://ss64.com/nt/syntax-strlen.html>
Loading...