Question:
I write a batch script and I want to verify that the user enters a date in correct format likeDD-MM-YYYY
.But I have a problem with a variable. I can display its value with the command
echo
, but the further processing of the variable´s value does not work as expected and so the if
condition doesn’t work, too. I don’t understand where the problem is!Here is my batch script:
if
doesn’t work. I don’t understand why %day%-%month%-%year%
is null.Answer:
1. Errors in batch file code
1.1 Usage of echo. to output an empty line
Please read the DosTips forum topic ECHO. FAILS to give text or blank line – Instead use ECHO/ and use in future
echo/
or within a FOR loop even better echo(
to output an empty line.1.2 No validation of user input string before further processing
Please read the chapter Usage of SET /P for user prompts in my answer on How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
1.3 Definition of an environment variable with name of a dynamic variable
Please open a command prompt, run
set /?
and read the output usage help. There is explained briefly also the dynamic variable DATE
of which current value can be accessed using the syntax for accessing environment variables.The following command line results in the definition of the environment variable
date
on user really inputs a string at all making it impossible to access the value of the dynamic variable DATE
after the execution of that command line and user input a string.See also: Difference between Dynamic Environment Variables and Normal Environment Variables in CMD
1.4 Spaces around equal sign on environment variable definition
The space character is a very important character for the Windows Command Processor
cmd.exe
processing batch files. Therefore the author of a batch file must take care on using spaces in a command line in the batch file.set date1 = %day%
is not correct to define an environment variable with name date1
with the string value of the environment variable day
as explained in full details by my answer onWhy is no string output with ‘echo %var%’ after using ‘set var = text’ on command line?
The referenced answer describes also very detailed why it is advisable to use the syntax
set "variable=value"
to prevent the definition of an environment variable with a trailing whitespace appended unwanted to the string value as done by the command line set month=%%b
inside the for /f
loop.1.5 String comparison with just one string enclosed in double quotes
The condition
if "%date%" == %day%-%month%-%year%
is never true because of the double quotes are taken into account on comparing the string left to the string comparison operator ==
with the string right to the comparison operator.Please see my answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files for full details about how a string comparison is done by the command IF respectively
cmd.exe
.1.6 Missing space character after second string in IF condition
The following command line is syntactically wrong because of a missing space:
%day%-%month%-%year%
, but there is also missing the space character left to (echo
which would result in interpreting %day%-%month%-%year%(echo
as second string to compare with "%date%"
and the rest of the line as command to execute.But this command line results in the execution of:
set month=%%b
is responsible for comparing the string "09-09-0999"
with the string 09-09
and interpreting -0999(echo yes) else (echo no)
as the command to execute on positive comparison as it can be seen on debugging the batch file. The condition is never true and for that reason the “command” is never executed which would otherwise result in an error message.2. Batch file for validation of input date
Here is a commented batch file to prompt a user for entering a date as long as not entering a valid date. The input date is verified by the batch file in a fail-safe and secure manner.
cls /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of the operators
||
and &
.If you have better answer, please add a comment about this, thank you!