• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home » Resolved: How to verify the format of a date in batch?

Resolved: How to verify the format of a date in batch?

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I write a batch script and I want to verify that the user enters a date in correct format like DD-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:
Here is the output of my batch file on execution:
You can see that the 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.
Do not use the name of a dynamic variable for an environment variable.
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 on
Why 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:
There are not only missing the double quotes around %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:
The trailing space on the command line 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.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
  • 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!

batch-file script
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Why reference in pointer array doesn’t have data?

24/03/2023

Resolved: EntityFramework creates/runs migrations using parameterless DataContext instance

24/03/2023

Resolved: Visual Studio 2022 crashes when using breakpoints

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.