You can do it in two lines, fully in a batch file, by writing the string to a file and then getting the length of the file. You just have to subtract two bytes to account for the automatic CR+LF added to the end.
Let's say your string is in a variable called strvar
:
ECHO %strvar%> tempfile.txt
FOR %%? IN (tempfile.txt) DO ( SET /A strlength=%%~z? - 2 )
The length of the string is now in a variable called strlength
.
In slightly more detail:
FOR %%? IN (filename) DO ( ...
: gets info about a fileSET /A [variable]=[expression]
: evaluate the expression numerically%%~z?
: Special expression to get the length of the file To mash the whole command in one line:
ECHO %strvar%>x&FOR %%? IN (x) DO SET /A strlength=%%~z? - 2&del x