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-27 22:43
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] Could you experts please help improve this batch program: validate date legality View 630 Replies 3
Original Poster Posted 2009-02-09 00:33 ·  中国 天津 电信
新手上路
Credits 6
Posts 3
Joined 2009-02-08 11:01
17-year member
UID 138312
Gender Male
Status Offline
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!

@echo off
:start
set /p input=请输入月份:
::a
set /p date=请输入日期:
::b
echo
pause
goto start

end


[ Last edited by HAT on 2009-2-9 at 02:13 ]
Floor 2 Posted 2009-02-09 00:55 ·  中国 广东 江门 电信
中级用户
★★
Credits 338
Posts 175
Joined 2007-10-21 15:30
18-year member
UID 100351
Gender Male
Status Offline
Use findstr together with it?
Floor 3 Posted 2009-02-09 01:21 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
@echo off
:start
set /p input=Please enter the month :
echo %input%|findstr /v ""&&(echo error&goto :start)
if %input% leq 0 (echo error&goto :start)
if %input% gtr 12 (echo error&goto :start)
:start_
set /p dt=Please enter the day :
echo %dt%|findstr /v ""&&(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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HAT +2 2009-02-09 02:13
Floor 4 Posted 2009-02-09 01:43 ·  中国 天津 电信
新手上路
Credits 6
Posts 3
Joined 2009-02-08 11:01
17-year member
UID 138312
Gender Male
Status Offline
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
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
moniuming +3 2009-02-09 09:54
Forum Jump: