<

Advanced Batch File Programming: TOMORROW.BAT

Published on
30,554 Points
19,954 Views
6 Endorsements
Last Modified:
Awarded
TOMORROW

TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?"

The crux of this batch file revolves around the XCOPY command - a technique I discovered while looking for an easy method for validating dates that avoids a series of complex mathematical procedures.  Here's the complete batch file:
::================================================
:: TOMORROW.BAT
::
:: Function to return tomorrow's date
::================================================
@echo off

set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%

:loop
  set /a d+=1

  if %d% gtr 31 (
    set d=1
    set /a m+=1
     
    if %m% gtr 12 (
      set m=1
      set /a y+=1
    )
  )
xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop

echo %d%/%m%/%y%

Open in new window



INPUT
The input section is pretty straightforward.  The code receives input from the system's %DATE% whose format is DD/MM/YYYY.

set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%

Open in new window

If your system's date format differs from DD/MM/YYYY then you will need to edit these three lines or provide some other method of delivering the date to the batch file in order for the rest of the code to work correctly.

You are reminded: This is an advanced topic and therefore, I would expect you to know how to do that.
When assigning values to variables, we use using SET /A (rather than just SET) to overcome the problem of attempting to perform octal arithmetic on '08' and '09' later on. This is because leading zeros are trimmed off during arithmetic assignment.


INCREMENTING DAY
Incrementing the day itself is a doddle.  Basically, '1' is added to the current day value. If the resultant date is valid, it is returned to the user.  

The trick is in the end-of-month handling.  The majority of times, the loop will execute just the once.  If today is the last day in the month, then:  If there are 30 days in the current month, the loop will execute twice.  In the worst-case scenario, where February 28 falls on a non-leap year, the loop will execute just four times; to wit: 28+1 is invalid, 29+1 is invalid, 30+1 is invalid, 31+1 becomes 1, which is valid).

:loop
  set /a d+=1

  if %d% gtr 31 (
    set d=1
    set /a m+=1

    if %m% gtr 12 (
      set m=1
      set /a y+=1
    )
  )
xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop

Open in new window

In all cases, should the day value exceed the maximum range a day may reach - which is 31, it is reset to '1' and the month value is incremented. The same principle is then applied to the month value should it exceed 12.


VALIDATING THE DATE
For information on validating dates using the XCOPY command please see my article: Advanced Batch File Programming: Validating Dates

If the date in '/d:' is valid and resolvable, then XCOPY continues (to do 'nothing') and control is passed to the next line of code in the batch file.  If, on the other hand, the date is invalid, the '||' (logical OR operator) passes control to the GOTO instruction and the process is repeated by jumping back the beginning of the loop.


OUTPUT
Output is straight forward. No real need to assign it to a variable such as tomorrow although you can if you wish to.

echo %d%/%m%/%y%

Open in new window

Notice the order in which D, M and Y are assembled - they match the input.

Also note that either D or M may consist of one or two digits. If you want to output this as DD/MM/YYYY then you will need to add the leading zeros yourself. For example, the following should do the job quite nicely:

set d=0%d%
set m=0%m%

echo %d:~-2%/%m~-2%/%y%

Open in new window

And that's about all you need to know.


FURTHER CONSIDERATIONS
As it stands, the batch file's only output is a date in D/M/YY format (or DD/MM/YY if you have added the leading zeros). This will work as a standalone batch file or as a function tacked onto the end of your own batch file and executed with a CALL statement.

As an external function, you might CALL it from within your own batch file like this:

for /f %%a in ('tomorrow') do set tomorrow=%%a

Open in new window

As a function added to your own code, you might do something like this:

for /f %%a in ('call :tomorrow') do set tomorrow=%%a

Open in new window

%%a is set to the date given by the function TOMORROW.BAT, this is then assigned to your variable tomorrow.

Output can also be redirected or piped into another command as in the following examples:

call tomorrow >file

Open in new window

or,
call tomorrow|find . . .

Open in new window

There is no reason why you cannot take input from the command line rather than DATE itself by replacing the three SET statements in the input section with something like this:

for /f "tokens=1-3 usebackq delims=/" %%a in ('%1') do (
  set d=%%a
  set m=%%b
  set y=%%c
)

Open in new window

Note: That would make the function return "The Day After x" rather than "Tomorrow"


RELATED ARTICLES
Advanced Batch File Programming: YESTERDAY.BAT
Advanced Batch File Programming: Validating Dates

EXTERNAL SOURCES
TOMORROW.BAT
YESTERDAY.BAT
Validating Dates

Further info may be found at http://www.paultomasi.co.uk/tomorrow
6
Author:Paul Tomasi
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free