计数CMD和batch file中的字母数量

我有这个项目使用batch file。下面的代码可以统计字符时,用户input(数字,字母,符号),但我希望程序只计算字母。 你能帮我解决这个问题吗? 我的代码如下所示。

@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 

 @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,即每个字符两个字节,通常是一个空字符和真实字符。

这个输出由一个过滤器处理, more或者find ,它会将空值作为行结束符处理,将输出字符串分成行,每行输入一行。

这组行被用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 

您将初始字符串长度设置为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