Skip to content

Instantly share code, notes, and snippets.

@ijprest
Created September 1, 2011 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijprest/1185398 to your computer and use it in GitHub Desktop.
Save ijprest/1185398 to your computer and use it in GitHub Desktop.
CMD Batch file implementation of strlen
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
call :strlen "%~1"
echo strlen("%~1") == !ERRORLEVEL!
goto :EOF
:strlen
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set __LEN=0
set __LEN_S=%~1
:strlen_loop
if "!__LEN_S!"=="" exit /B %__LEN%
for %%Q IN (32 16 8 4 2) DO if NOT "!__LEN_S:~%%Q!"=="" set /A __LEN=!__LEN!+%%Q&set __LEN_S=!__LEN_S:~%%Q!&goto :strlen_loop
set /A __LEN=!__LEN!+1&set __LEN_S=!__LEN_S:~1!&goto :strlen_loop
@ijprest
Copy link
Author

ijprest commented Sep 10, 2011

Should be relatively efficient (as these things go) because it tries to break off 32-character chunks at a time, instead of doing it character by character.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment