Monday, September 13, 2010

Compute length of a string in a batch file

set myvar="some string"
rem compute the length of a string
set #=%myvar%
set length=0
:loop
if defined # (
    rem shorten string by one character
    set #=%#:~1%
    rem increment the string count variable %length%
    set /A length += 1
    rem repeat until string is null
    goto loop
)
echo myvar is %length% characters long!

Thanks to poster Secret_Doom on computing.net

2 comments:

Note: Only a member of this blog may post a comment.