windows - How do you get the string length in a batch file? -
there doesn't appear easy way length of string in batch file. e.g.,
set my_string=abcdefg set /a my_string_len=???
how find string length of my_string
?
bonus points if string length function handles possible characters in strings including escape characters, this: !%^^()^!
.
as there no built in function string length, can write own function so:
@echo off setlocal set "mystring=abcdef!%%^^()^!" call :strlen result mystring echo %result% goto :eof :strlen <resultvar> <stringvar> ( setlocal enabledelayedexpansion set "s=!%~2!#" set "len=0" %%p in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) ( if "!s:~%%p,1!" neq "" ( set /a "len+=%%p" set "s=!s:~%%p!" ) ) ) ( endlocal set "%~1=%len%" exit /b )
this function needs 13 loops, instead of simple strlen function needs strlen-loops.
handles characters.
Comments
Post a Comment