中国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-09-30 11:31
47,813 topics / 349,918 posts / today 0 new / 48,279 members
DOS批处理 & 脚本技术(批处理室) » [Solved] Could you experts please help improve this batch program: validate date legality
Printable Version  754 / 3
Floor1 bsthq Posted 2009-02-09 00:33
新手上路 Posts 3 Credits 6
I just started learning batch files, and there are many things I don't understand how to implement. Could you experts help me improve the batch file below so it can meet the following requirements? Thank you!

I want to add batch statements at ::a and ::b to check whether the month and date are within range. The number cannot be 0, or greater than 12, or greater than 31. Letters and spaces must not be allowed as input either. If the input is wrong, return to start.
If there is a better way to handle this, I also hope the experts can suggest it. Thank you!



[ Last edited by HAT on 2009-2-9 at 02:13 ]
Floor2 BC Posted 2009-02-09 00:55
中级用户 Posts 175 Credits 338
Use findstr together with it?
Floor3 moniuming Posted 2009-02-09 01:21
银牌会员 Posts 574 Credits 1,335 From 广西
Floor4 bsthq Posted 2009-02-09 01:43
新手上路 Posts 3 Credits 6
Thanks, teacher. I tried it, and it can do what's needed, but if the value is empty and Enter is pressed, it still won't catch it, and it will continue executing. Following your method, I made the following changes:
i@echo off
:start
set input=
set /p input=Please enter month [1-12]:
if "%input%"=="" goto start
echo %input%|findstr /v "[0-9]"&&(echo error&goto :start)
if %input% leq 0 (echo error&goto :start)
if %input% gtr 12 (echo error&goto :start)
:start_
set dt=
set /p dt=Please enter date [1-31]:
if "%dt%"=="" goto :start_
echo %dt%|findstr /v "[0-9]"&&(echo error&goto :start_)
if %dt% lss 1 (echo error&goto :start_)
if %dt% geq 32 (echo error&goto :start_)
echo Month:%input% Day:%dt%
goto :start
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023