@ECHO OFF
setlocal enableextensions disabledelayedexpansion
:getString
echo(
REM Set "string" variable
set "string="
SET /p "string=Type characters to Count:"
:calculateLength
if not defined string ( set "str_len=0" ) else (
for /f %%a in ('
cmd /u /v /q /c"(echo(!string!)" %= output unicode string =%
^| find /v "" %= separate lines =%
^| findstr /r /c:"[a-zA-Z]" %= filter characters =%
^| find /c /v "" %= count remaining lines =%
') do set "str_len=%%a"
)
:show
REM Echo the actual string value and its length.
ECHO [%string%] is %str_len% letters long!
echo(
set /p "prompt=Do you want to continue? Y/n: " || set "prompt=Y"
if /i "%prompt%"=="Y" goto :getString
pause
exit /b
This uses a simple trick. If we start a unicode cmd instance, its output (the result from echo in this case) is unicode, that is, two bytes per character, usually a null and the real character.
This output is processed by a filter, more or find, that will process the nulls as line terminators, splitting the output string into lines, one line for each input character.
This set of lines are filtered with findstr to only allow letters. The remaining lines are counted with a find /c /v "" command that will count non empty lines.
edited to adapt to comments.
Input a block of lines and process them
@ECHO OFF
setlocal enableextensions disabledelayedexpansion
set "tempFile=%temp%\%random%%random%%random%.tmp"
:getString
echo(
echo(Please, type your text and end input pressing F6 and Enter
copy con "%tempfile%" >nul 2>nul
:calculateLength
for /f %%a in ('
cmd /u /q /c"(type "%tempFile%")" %= output unicode string =%
^| find /v "" %= separate lines =%
^| findstr /r /c:"[a-zA-Z]" %= filter characters =%
^| find /c /v "" %= count remaining lines =%
') do set "str_len=%%a"
:show
ECHO input data is %str_len% letters long!
echo(
del /q "%tempFile%" 2>nul
set /p "prompt=Do you want to continue? Y/n: " || set "prompt=Y"
if /i "%prompt%"=="Y" goto :getString
pause
exit /b