Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

Advanced Batch File Programming: Validating Dates

Published:
Updated:
VALIDATING DATES

One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the following:

echo.|date %d%/%m%/%y% >nul
if %errorlevel% gtr 0 ... etc.
The problem with this approach is it changes the system's global date setting and this can be risky in a multitasking environment. Even if the current date were to be preserved in temporary variable and then restored afterwads as in the following exmaple, the possibility of a midnight rollover cannot be ignored as this would throw the system's date out by a whole day or so.

set current_date=%date%
echo.|date %d%/%m%/%y% >nul
if %errorlevel% gtr 0 ... etc.
date %current_date%
The solution is to use the XCOPY command. This takes the following format:

XCOPY /D:m-d-y /L "source" "destination" >NUL 2>&1
For XCOPY to do it's job properly, all of its command line parameters and switches must be in good order because the technique described in this article relies on XCOPY's error condition to determine whether the date in '/D:m-d-y' is valid or not. This error must therefore come from 'm-d-y' alone and no other parameter. Moreover, all output is redirected to the NUL device hence the '>nul 2&>1' so errors will be difficult to detect - both syntactical and logical.

The parameters for XCOPY are:

/D:<date in m-d-y format>
/L
<source>
<destination>
<redirection>
The XCOPY command does not copy any files nor make changes to any drives because of the '/L' switch which merely tells XCOPY to (L)ist those files that would normally be copied if the '/L' switch had been omitted.


/D:
Pay particular attention to XCOPY's '/D:m-d-y' format. It must be syntactically correct and be in this order: 'm', 'd' and 'y'.

The forwardslash ('/') character can be used in place of the hyphen ('-') and this is a matter personal choice.


/L
As already explained, the '/L' parameter displays files that would be copied.


SOURCE
As a bare minimum XCOPY requires a valid source and destination filespec.

The source file needs to exist, whether implied or otherwise. The only filespecs that exist for certain are '.', '..' and '%0'. Any one of these can be used as the source.

When using '%0' it should be specified as "%~0" (including the double-quotes) to avoid possible errors arising from any spaces in the filename or the file's path.


DESTINATION
As a bare minimum XCOPY requires a valid source and destination filespec.

As with source, destination can be '.', '..' or '%0' however, they cannot be the same. So, if source is '.' then destination can only be '..' or '%0'.

When using '%0' it should be specified as "%~0\" (including the double-quotes) to avoid possible errors arising from any spaces in the filename or the file's path.

Note the inclusion of the backslash ('\') character. This avoids ambiguity between filenames and directory names. If this is not included XCOPY will pause for confirmation. The problem with this is, apart from the obvious desire to execute XCOPY non-interactively, output is redirected to the NUL device so there will be no visible signs as to why the batch file has seemingly hung.

On a presonal note, I prefer to use '.' for source and '..' for destination purely because it involves less code.


REDIRECTION
Redirection consists of '>NUL' and '2>&1' ensuring both STDOUT and STDERR are redirected so there is no output to the screen.

CONCLUSION
Putting all this together, the full command might look something like the following:

XCOPY /D:%m%-%d%-%y% /L . .. >NUL 2>&1

EXAMPLES
Try running the following batch files with different values for 'd', 'm' and 'y'.

::================================================
                      :: EX1.BAT
                      ::
                      :: Demonstrating XCOPY as a date validator
                      ::================================================
                      @echo off
                      
                      set "d=23"
                      set "m=10"
                      set "y=2012"
                      
                      xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1
                      
                      if %errorlevel% equ 0 (
                        echo Valid Date
                      ) else (
                        echo Invalid Date
                      )

Open in new window


::================================================
                      :: EX2.BAT
                      ::
                      :: Demonstrating XCOPY as a date validator
                      ::================================================
                      @echo off
                      
                      set "d=23"
                      set "m=10"
                      set "y=2012"
                      
                      xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 && echo Valid Date || echo Invalid Date

Open in new window


::================================================
                      :: EX3.BAT
                      ::
                      :: Demonstrating XCOPY as a date validator
                      ::================================================
                      @echo off
                      
                      set "d=23"
                      set "m=10"
                      set "y=2012"
                      
                      xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto :eof
                      
                      echo Valid Date

Open in new window


RELATED ARTICLES
Advanced Batch File Programming: TOMORROW.BAT
Advanced Batch File Programming: YESTERDAY.BAT


EXTERNAL SOURCES
TOMORROW.BAT
YESTERDAY.BAT
Validating Dates
4
7,175 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.