<

Advanced Batch File Programming: Validating Dates

Published on
15,052 Points
5,152 Views
4 Endorsements
Last Modified:
Awarded
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
Comment
Author:Paul Tomasi
0 Comments

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Join & Write a Comment

When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
A query can call a function, and a function can call Excel, even though we are in Access. This is Part 2, and steps you through the VBA that "wraps" Excel functionality so we can use its worksheet functions in Access. The declaration statement de…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month