Previous / yesterday’s date in DOS batch file
Here is the script to get previous date output in DOS using bat files. This is very useful when we take backup of files with previous date name. (for example backup_daily_20072010.zip ) or can set bat files to get last 7 days backup to a zip. According to your requirement you can get any previous date from this script. See the variable values. Following Script just show today’s and yesterdays date. Most of the script having some problems when it run on 1st if any moth. but here it is solved. you can customize the date formats also. using variables %DD%, %MM%, %YYYY% Please comment to this post If you have any query .
@echo off
for /f "tokens=1" %%i in ('date /t') do set thedate=%%i
set mm=%thedate:~3,2%
set dd=%thedate:~0,2%
set yyyy=%thedate:~6,4%
echo.
echo.
echo Today : %dd%.%mm%.%yyyy%
echo.
if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )
if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )
set /A dd=%dd% - 1
set /A mm=%mm% + 0
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1
:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31
goto ERROR
:SET31
set /A dd=31 + %dd%
goto DONE
:SET30
set /A dd=30 + %dd%
goto DONE
:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto DONE
:SET29
set /A dd=29 + %dd%
:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
set YESTERDAY=%dd%.%mm%.%yyyy%
echo.
echo YESTERDAY : %YESTERDAY%
echo.
echo.
pause







September 1st, 2010 at 6:56 PM
How do I use this script, I have tried to put it into a dos .cmd file but the dos screen just comes up and disappears rather than stay open at the pause prompt as I thought it would. I need to use this script to copy some files of certain dates to different folders etc.
September 2nd, 2010 at 1:50 PM
Hi Michael,
That problem happens due to copy paste that code from this page. The formatting for code is gone, Now that is fixed so you can copy paste the code
September 2nd, 2010 at 2:10 PM
Ah that seems OK but just getting an error on YESTERDAY for some reason, TODAY is OK but YESTERDAY puts an extra 0 in the month, see output below.
Today : 02.09.2010
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0×11), or octal (021).
YESTERDAY : 01.009.2010
Press any key to continue . . .
Thanks in advance for your help.
September 2nd, 2010 at 2:14 PM
There is a bug in the code.
It will not work properly If the month is August or September. The bug is fixed with following code. place this code before line ” set /A dd=%dd% – 1 ”
Code :
if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )
September 2nd, 2010 at 2:18 PM
Fantastic thanks very much, very strange bug isn’t it?
September 2nd, 2010 at 2:20 PM
@ Arun,
Thanks for your support,
The bug fixed in above code.
September 8th, 2010 at 1:57 PM
Arun
The code worked ok for a week but has now reverted to the problem with the day coming as 008, see below.
Today : 08.09.10
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0×11), or octal (021).
YESTERDAY : 008.09.10
Press any key to continue . . .
mt today 0080910
Press any key to continue . . .
Here is the code I have in my script, any ideas why this has done this again?
@echo off
for /f “tokens=1″ %%i in (‘date /t’) do set thedate=%%i
set mm=%thedate:~3,2%
set dd=%thedate:~0,2%
set yy=%thedate:~8,2%
echo.
echo.
echo Today : %dd%.%mm%.%yy%
echo.
if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )
set /A dd=%dd% – 1
set /A mm=%mm% + 0
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% – 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yy=%yy% – 1
:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31
goto ERROR
:SET31
set /A dd=31 + %dd%
goto DONE
:SET30
set /A dd=30 + %dd%
goto DONE
:LEAPCHK
set /A tt=%yy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yy% %% 400
if %tt%==0 goto SET29
:SET28
set /A dd=28 + %dd%
goto DONE
:SET29
set /A dd=29 + %dd%
:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
set YESTERDAY=%dd%.%mm%.%yy%
echo.
echo YESTERDAY : %YESTERDAY%
echo.
echo.
PAUSE
ECHO mt today %DD%%MM%%YY%
pause
September 8th, 2010 at 3:53 PM
@Michael
Yes we diagnosed the problem, it was a problem with values 8 & 9 . this problem repeats today ( 08-09-2010). we already set an option for month 08 & 09. we will do the same thing here also.
Thanks for notifying us the errors
Code is updated
September 8th, 2010 at 4:07 PM
I just added the following after the fix for the month issue, seems to be OK
if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )
September 8th, 2010 at 4:10 PM
@Michael
yes same thing.
I updated in the above code.
April 2nd, 2011 at 5:45 PM
good discussion going on here… thanks to all of you and ALBIN for the interesting post…:0
April 12th, 2011 at 5:13 PM
Great! Simply Great! Thank you for this script. Now it is easy to make backups of logs in WINDOWS environment.
June 8th, 2011 at 8:56 AM
thanks, this is extremely useful
June 15th, 2011 at 10:47 PM
The above batch file is not working and giving below output
Today : 16.Ju.-11
YESTERDAY : 15.00.-11
I want the output like
15-06-2011
June 16th, 2011 at 9:48 AM
As per the today output I assume that your computers date format is as below
“dd.MMM.yy”
The script will give output if the date format is “dd.MM.yyyy”
Change your date format to above format and try,
June 18th, 2011 at 12:58 PM
Thanks a lot Albin, its working fine now,
I have one more question can I change the date format of my machine for English United States M/d/yyyy
Please let me know and update or send the script to my mail id
contactahmad@rediffmail.com
July 8th, 2011 at 11:00 AM
Hi
Any one can help us
XCOPY D:\*.* E:\”%date:~7,2%-%date:~4,2%-%date:~10,4%” /D:7-2-2011 /s /c /i /y
here i need to enter /D:7-2-2011 manually every day
any one help us by modifing auto matically taking 2 days back date
July 8th, 2011 at 11:01 AM
its very urgent
July 8th, 2011 at 9:43 PM
Can you explain your requirement ? where you want to get the output ?
July 11th, 2011 at 11:11 AM
Hi Albin
/D:7-2-2011 this area needs to take date 2 days back from sys date
thanks
nagi
July 11th, 2011 at 3:30 PM
Source:D:\*.*
Dest :E:\”%date:~7,2%-%date:~4,2%-%date:~10,4%”(It creats folder with SYS date)
/D /s /c /i /y(/D) Date modified if i menction date(M-D-YY) it will copy only modifies files dated on & after the date.this date i need to type every 2 days .
exp
if i run the batch file tday(7-11-2011), i need to setup last 2 days date:/D(7-9-2011)
like this every 2days i need to change the date
thanks
nagi
July 12th, 2011 at 2:12 PM
Here we are using date format “DD.MM.YYYY”
You can modify following line as per your format
For two day backs result change following line
July 13th, 2011 at 11:12 AM
If passible can you please send the txt file to my mail id
nags.basis@gmail.com
July 27th, 2011 at 2:10 PM
I feel the line of “goto ERROR” useless, isn’t it?
August 12th, 2011 at 10:57 PM
Hi albin i have a problem with brackets in the if parts
<code>
if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )
if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )
</code>
there is a error like this
Today: ..
Not expected ( at this time.
which can be the problem.
Thanks in advance