::================================================
:: 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%
set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%
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.
: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
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.
echo %d%/%m%/%y%
Notice the order in which D, M and Y are assembled - they match the input.
set d=0%d%
set m=0%m%
echo %d:~-2%/%m~-2%/%y%
And that's about all you need to know.
for /f %%a in ('tomorrow') do set tomorrow=%%a
As a function added to your own code, you might do something like this:
for /f %%a in ('call :tomorrow') do set tomorrow=%%a
%%a is set to the date given by the function TOMORROW.BAT, this is then assigned to your variable tomorrow.
call tomorrow >file
or,
call tomorrow|find . . .
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
)
Note: That would make the function return "The Day After x" rather than "Tomorrow"
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.
Comments (5)
Author
Commented:Commented:
INfact this article is useful,i did seen many peopls asking for this question on different forums,
I have one question, Is it date format dependent ?
On my windows 7 machine it just say :Missing Oprend".
When i changed Echo off to Echo on, it seems going into infinite loop.
My date format is Sun 02/06/2011.
Also will it work on months with less then 31 days like Feb and Apr, Or needs to be modified to work with all months..May be you can add the code to check the month and get the total number of days automaticly..
Thank you .
Author
Commented:There is no need to modify the code to handle different number of days for different months, the validator will accept ANY combination of date numbers as it is.
With regard to your date format, DDD MM/DD/YYYY, change these lines at the start of the program:
set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%
to:
set /a d=%date:~7,2%
set /a m=%date:~4,2%
set /a y=%date:~10,4%
Commented:
Steve
Author
Commented:I'm currently doing another write-up (like the many in the making). I see it's strength in validating dates input by the user.