计算 CMD 和批处理文件中的字母数

Count number of letters in CMD and batchfile

我有这个项目使用批处理 file.My 下面的代码可以在用户键入输入(数字、字母、符号)时计算字符数,但我希望程序只计算字母数。你能帮我解决这个问题吗?我的代码如下所示。

@ECHO OFF
echo.

:a
REM Set "string" variable
SET /p string="Type characters to Count:"
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_len=1

:loop
if defined temp_str (
REM Remove the first character from the temporary string variable and increment 
REM counter by 1. Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
SET /A str_len += 1
GOTO loop
)

REM Echo the actual string value and its length.
ECHO %string% is %str_len% characters long!
set /p prompt="Do you want to continue?Y/N:"
if %prompt%==Y goto a
goto b

:b
pause
exit

您在行中将初始字符串长度设置为 1

SET str_len=1

像这样将其更改为 0:

SET str_len=0
@ECHO OFF
setlocal EnableDelayedExpansion

REM Define a string with all letters:
SET letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ

echo.

:a
REM Set "string" variable
SET /p string="Type characters to Count:"
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_letters=0

:loop
if defined temp_str (
REM Get the first character from temporary string variable
SET char=%temp_str:~0,1%
REM If the character is letter, count it
if "!letters:%char%=!" neq "%letters%" SET /A str_letters += 1
REM Remove the first character from the temporary string variable.
REM Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
GOTO loop
)

REM Echo the actual string value and its number of letters.
ECHO %string% have %str_letters% letters
set /p prompt="Do you want to continue?Y/N:"
if %prompt%==Y goto a
goto b

:b
pause
exit
@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

这使用了一个简单的技巧。如果我们启动一个 unicode cmd 实例,它的输出(在这种情况下来自 echo 的结果)是 unicode,即每个字符两个字节,通常是空字符和真实字符。

此输出由过滤器 morefind 处理,它将空值作为行终止符处理,将输出字符串分成多行,每个输入字符一行。

这组行被findstr过滤为只允许字母。剩余的行使用 find /c /v "" 命令计数,该命令将计算非空行。

编辑以适应评论。

输入一行并进行处理

@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