26} How do I get the length of a string into a variable?
There are many variations to get a string's length
@echo off & setlocal enableextensions
set testString_=This is a test string
call :GetStrLength "%testString_%" length_
echo 123456789 123456789 123456789
echo.%testString_%
echo Length = %length_%
goto :EOF
endlocal & goto :EOF
::
::===============================================================
:: Subroutine: Return the length of a string
:GetStrLength string count
setlocal enableextensions enabledelayedexpansion
if "%~1"=="" (endlocal & set "%2=0" & goto :EOF)
set s=%~1
for /L %%c in (0,1,255) do (
set si=!s:~%%c,1!
if defined si set charCount=%%c
)
set /a charCount+=1
endlocal & set "%2=%charCount%" & goto :EOF
The output is
C:\_D\TEST>cmdfaq
123456789 123456789 123456789
This is a test string
Length = 21
If there are leading or trailing blanks in the string they are
included, as they logically should.
Alternatively
@echo off & setlocal enableextensions enabledelayedexpansion
::
:: Get a test string
if "%~1"=="" (
set s=This is a test string
) else (
set s=
%*
)
::
:: Get the length of the quoted string assuming a max of 255
set charCount=0
for /l %%c in (0,1,255) do (
set si=!s:~%%c!
if defined si set /a charCount+=1)
if %charCount% EQU 256 set charCount=0
echo The length of "%s%" is %charCount% characters
endlocal & goto :EOF
The output could be e.g.
C:\_D\TEST>cmdfaq Hello World!
The length of "Hello World" is 11 characters
Note the (undesirable) effect of the ! character. The length of "Hello
World!" is 12 characters.
As for
%* "the %* batch parameter is
a wildcard reference to all the arguments, not including %0, that are
passed to the batch file".
A Visual Basic Script (VBScript) aided command line script solution
@echo off & setlocal enableextensions disabledelayedexpansion
set testString_=This is a test string!
call :GetStrLength "%testString_%" length_
echo 123456789 123456789 123456789
echo.%testString_%
echo Length = %length_%
goto :EOF
endlocal & goto :EOF
::
::===============================================================
:: Subroutine: Return the length of a string using VBScript
:GetStrLength
setlocal enableextensions
>"%temp%\tmp$$$.vbs" echo WScript.Echo Len("%~1")
for /f "tokens=* delims=" %%a in (
'cscript //nologo "%temp%\tmp$$$.vbs"') do set return_=%%a
for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
endlocal & set "%~2=%return_%" & goto :EOF
The output is
C:\_D\TEST>cmdfaq
123456789 123456789 123456789
This is a test string!
Length = 22
One method for getting the length of the string is to truncate a
copy of the string one character at a time (from the front) until
the copy becomes undefined, while at the same time keeping score of
the number of steps required. The possibility of an already empty
string must be taken into account.
@echo off
setlocal enableextensions
set testString_=This is a test string!
call :GetStrLength %testString_%
echo 123456789 123456789 123456789
echo.%testString_%
echo Length = %length_%
goto :EOF
::
::===============================================================
:GetStrLength
setlocal enableextensions
set rest_=%*
set /a count_=0
if not defined rest_ (set /a count_=-1 & set rest_=X)
:_loop
set /a count_+=1
set rest_=%rest_:~1%
if defined rest_ goto :_loop
endlocal & set length_=%count_% & goto :EOF
C:\_D\TEST>cmdfaq
123456789 123456789 123456789
This is a test string!
Length = 22
Yet another option is to write the string into a temporary file and
get that file's size (minus 2).
@echo off & setlocal enableextensions
set testString_=This is a test string!
echo %testString_%>"%temp%\length.tmp"
for %%f in ("%temp%\length.tmp") do set length_=%%~zf
for %%f in ("%temp%\length.tmp") do if exist %%f del %%f
set /a length_ = %length_% - 2
echo The length of "%testString_%" is %length_%
endlocal & goto :EOF
The output is
D:\TEST>cmdfaq
The length of "This is a test string!" is 22
Or
@echo off & setlocal enableextensions
set testString_=This is a test string!
0>nul 1>"%temp%\cmdfaq.tmp" set /p var_="%testString_%"
for %%f in ("%temp%\cmdfaq.tmp") do (
set length_=%%~zf
if exist %%f del %%f)
echo The length of "%testString_%" is %length_%
endlocal & goto :EOF
The output is again
D:\TEST>cmdfaq
The length of "This is a test string!" is 22
A
G(nu)AWK solution to the problem is
@echo off & setlocal enableextensions
set testString_=This is a test string!
if not exist c:\mytemp mkdir c:\mytemp
echo %testString_%|
unxgawk^
"{printf\"@set length_=%%s\n\",length()}"^
>c:\mytemp\length$$.cmd
call c:\mytemp\length$$.cmd
echo 123456789 123456789 123456789
echo.%testString_%
echo Length = %length_%
set length_=
del c:\mytemp\length$$.cmd
rmdir c:\mytemp
endlocal & goto :EOF
C:\_D\TEST>cmdfaq
123456789 123456789 123456789
This is a test string!
Length = 22
A FINDSTR option, based on Usenet postings in alt.msdos.batch.nt
@echo off & setlocal enableextensions
set testString_=This is a test string!
for /f "tokens=1 delims=:" %%a in (
'^(echo.%testString_%^&echo AnUncommonString^)^|
findstr /o "AnUncommonString"'
) do set /a length_=%%a-3
echo.%testString_%
echo Length = %length_%
endlocal & goto :EOF
C:\_D\TEST>cmdfaq
This is a test string!
Length = 22
Another rendition of a FINDSTR solution
@echo off & setlocal enableextensions
set testString_=This is a test string!
for /f "delims=:" %%a in ('
^(echo."%testString_%"^& echo.^)^|findstr /o .'
) do set /a length_=%%a-5
echo The length of "%testString_%" is %length_%
endlocal & goto :EOF