China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 13:16
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to judge whether the input is a number or a letter in batch processing? View 4,469 Replies 6
Original Poster Posted 2006-11-24 07:48 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 182
Posts 75
Joined 2006-10-11 00:10
19-year member
UID 65220
Gender Male
Status Offline
As the question says..
For example set /p a=Number or letter

How to write it?? (It's a randomly entered number)
Floor 2 Posted 2006-11-24 08:38 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
I thought that using the regular expression of findstr would easily complete the task, but unexpectedly there was still a bug: when the last character of the string is the escape character ^, an incorrect judgment result will appear:

@echo off
:begin
cls
set input=
set /p input= Please enter a string:
if "%input%"=="" goto begin
echo.
echo %input%|findstr "^*$">nul && echo The string you entered is purely numeric||(
echo %input%|findstr "^*$">nul && echo The string you entered is purely alphabetic||echo The string you entered is neither purely numeric nor purely alphabetic
)
echo.
pause
goto begin
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +3 2006-11-24 11:23
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-11-24 08:52 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 182
Posts 75
Joined 2006-10-11 00:10
19-year member
UID 65220
Gender Male
Status Offline
Thank you, moderator大哥. I just tried it and it works very well...

I just don't know what the code means, heh heh...

Can you take some time to explain it... Thanks.
Floor 4 Posted 2006-11-24 11:28 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Wonderful~~Learned from the moderator~~ :))))
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 5 Posted 2006-11-24 12:52 ·  中国 浙江 温州 电信
中级用户
★★
Credits 458
Posts 196
Joined 2006-10-05 12:04
19-year member
UID 64614
Status Offline
The simplest way, compare variable with number. If the variable is a number, the comparison will succeed. If the variable is not a number, the comparison fails.

if %a% GTR 0 echo %a% is a number
Floor 6 Posted 2006-11-24 14:01 ·  中国 湖南 长沙 电信
初级用户
★★
Credits 182
Posts 75
Joined 2006-10-11 00:10
19-year member
UID 65220
Gender Male
Status Offline
zerocq Please write out all of them, okay??

I'm a noob, only this sentence I don't understand~~
Floor 7 Posted 2006-11-24 22:19 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Direct comparison with numbers can cause errors! Test code:
@echo off
set /p num=Please enter a number:
if %num% geq 1 (echo YES) else echo NO
pause>nul
When you enter 10a, it returns YES!
I think there are two solutions:
1. findstr + regular expression, the previous moderator has used it;
2. Use the particularity of set /a:
For example: set /a num=10a, then num cannot be defined correctly. If it starts with a letter, the return result of num is 0. If it is pure numbers, it returns the original number. Using this feature, it can be used to roughly detect the input situation.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +3 2006-11-24 22:27
Forum Jump: