Here is the code I wrote:
@echo off
:top
set chkScheduleTime=""
set chkScheduleName=""
set /p scheduleName=Enter the name of the schedule?
cls
set /p scheduleTime=Enter the time to schedule task?
cls
set chkScheduleTime=%scheduleTime =%
set chkScheduleName=%scheduleName =%
IF /I "%chkScheduleName%"=="" (
set errorMessage=*Name of schedule required
)
IF /I "%chkScheduleTime%"=="" (
set errorMessage=%errorMessage% *Time of schedule required
)
IF NOT defined "%errorMessage%" (
echo %errorMessage%
pause
cls
goto top
)
echo Schedule Name %scheduleName%
echo.
echo Time: %scheduleTime%
:end
What I am trying to do is accept two parameters from the user which are the task schedule name and the time of the task. Then the program would check the the variables if they are empty and display an error for the variable that is empty, then prompt the user to re-enter the variable (if one or both were not entered). However the program is displaying the following even if the user entered the schedule name and time:
*Name of schedule required *Time of schedule required
Also note the following part of the code is used to help check if the values entered by the user are only spaces. Thus its the equivalent of Trim() in regular programming:
set chkScheduleTime=%scheduleTime =%
set chkScheduleName=%scheduleName =%
You need to use
to strip the spaces from
scheduleTime.Also you never reset
errorMessage, so that once you have an error it won’t go away.Also I think you might want to use
if defined errorMessage.