Software >> OS >> Windows >> Command Line >> How to calculate length of a string in batch file

STRLEN.BAT

SET X=1234
SET Y=-1234
CALL :strLen X strLenX
CALL :strLen Y strLenY
ECHO.%X% is %strLenX% characters long
ECHO.%Y% is %strLenY% characters long
GOTO :eof

:strLen
setlocal enabledelayedexpansion
:strLen_Loop
   if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
GOTO :eof

Sample output