中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-10 16:00
47,811 topics / 349,897 posts / today 0 new / 48,255 members
DOS批处理 & 脚本技术(批处理室) » How to judge whether the input is a number or a letter in batch processing?
Printable Version  4,602 / 6
Floor1 online365 Posted 2006-11-24 07:48
初级用户 Posts 75 Credits 182
As the question says..
For example set /p a=Number or letter

How to write it?? (It's a randomly entered number)
Floor2 namejm Posted 2006-11-24 08:38
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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:
Floor3 online365 Posted 2006-11-24 08:52
初级用户 Posts 75 Credits 182
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.
Floor4 redtek Posted 2006-11-24 11:28
金牌会员 Posts 1,147 Credits 2,902
Wonderful~~Learned from the moderator~~ :))))
Floor5 zerocq Posted 2006-11-24 12:52
中级用户 Posts 196 Credits 458
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
Floor6 online365 Posted 2006-11-24 14:01
初级用户 Posts 75 Credits 182
zerocq Please write out all of them, okay??

I'm a noob, only this sentence I don't understand~~
Floor7 youxi01 Posted 2006-11-24 22:19
高级用户 Posts 247 Credits 846 From 湖南==》广东
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.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023