Windows 计算CMD和batchfile中的字母数

Windows 计算CMD和batchfile中的字母数,windows,batch-file,cmd,Windows,Batch File,Cmd,我有这个项目使用批处理文件。我下面的代码可以计数字符时,用户输入(数字,字母,符号),但我希望该程序只计数字母。你能帮我解决这个问题吗。我的代码如下所示 @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_st

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

@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关闭
setlocal enableextensions disabledelayedexpansion
:getString
回音(
REM设置“字符串”变量
设置“字符串=”
SET/p“string=键入要计数的字符:”
:计算长度
如果未定义字符串(设置为“str_len=0”),则为(
对于/f%%a英寸('
cmd/u/v/q/c“(echo(!string!)”%=输出unicode字符串=%
^|查找/v”“%=单独的行=%
^|findstr/r/c:“[a-zA-Z]”%=筛选字符=%
^|查找/c/v”“%=剩余行数=%
“)设置“str_len=%%a”
)
:显示
REM回显实际字符串值及其长度。
回音[%string%]的长度为%str\u len%个字母!
回音(
set/p“prompt=是否继续?Y/n:| | set“prompt=Y”
如果/i“%prompt%”==“Y”转到:getString
暂停
退出/b
这使用了一个简单的技巧。如果我们启动一个unicode cmd实例,它的输出(在本例中是来自
echo
的结果)是unicode,即每个字符两个字节,通常是空字符和实字符

此输出由过滤器
more
find
处理,该过滤器将空值作为行终止符进行处理,将输出字符串拆分为行,每个输入字符一行

使用
findstr
对这组行进行过滤,以仅允许使用字母。使用将计数非空行的
find/c/v”“
命令对其余行进行计数

编辑以适应评论

输入一组行并进行处理

@ECHO关闭
setlocal enableextensions disabledelayedexpansion
设置“tempFile=%temp%\%random%%random%%random%%random%.tmp”
:getString
回音(
echo(请键入文本并按F6结束输入,然后输入
复制con“%tempfile%”>nul 2>nul
:计算长度
对于/f%%a英寸('
cmd/u/q/c“(类型“%tempFile%””%=输出unicode字符串=%
^|查找/v”“%=单独的行=%
^|findstr/r/c:“[a-zA-Z]”%=筛选字符=%
^|查找/c/v”“%=剩余行数=%
“)设置“str_len=%%a”
:显示
回显输入数据的长度为%str_len%个字母!
回音(
删除/q“%tempFile%”2>nul
set/p“prompt=是否继续?Y/n:| | set“prompt=Y”
如果/i“%prompt%”==“Y”转到:getString
暂停
退出/b

它计算字符数,但计算错误。你有什么更好的主意吗?是的,我已经更改了值,但我只想计算字母数,我该怎么做?谢谢我运行了你的代码,它给出了正确的字符数。非常感谢你的回答,但它还需要阅读空格,包括换行符。你能帮我一下吗。
@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