Wednesday, July 28, 2010

Get Yesterday's date in MS DOS Batch file

A while back while I was trying to figure out the best way to gather some log files from Amazon S3 buckets and some web servers I run.  These resources are currently generating around 10-15GB of uncompressed log files daily.  Besides being fairly large in size the S3 (and CloudFront) log files are numerous.  Any given bucket can easily generate 1,000 or more log files per day - that's a whole other story. . .

Anyway, I wanted to be able to run a process sometime after midnight that would gather and zip the previous day's files and stash the zipped files in another location for archival.  It's pretty easy to calculate the previous day's date if it's in the middle of the month, but what if it's the first of the month, first of the year, and what about leap year, etc., etc. . . ?  So I searched around the web a bit and came across a great solution to this issue on Experts Exchange (Get Yesterday date in MS DOS Batch file).  Thanks to SteveGTR for this one.

I have modified the original script a bit to suite my needs.  Most notably at the end of the script I create two variables, IISDT and AWSDT, to match IIS and Amazon Web Services (S3 and CloudFront) log formats, respectively.  I use this in a simple batch file which is executed like, "gather_log_files.bat 1."  The number "1" is passed into the script which calculates the date of "1" day before the current date.  Of course you could pass any number in there to generate a date x days in the past.  It's very slick. NOTE: If you don't specify a number after the batch file "1" is assumed.

So, without further ado, here's the script.
@echo off

set yyyy=

set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))

if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

set CurDate=%mm%/%dd%/%yyyy%
set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

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

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
REM ** Month 12 falls through

set /A dd=31 + %dd%

set /A dd=30 + %dd%

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

set /A dd=28 + %dd%

set /A dd=29 + %dd%

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

REM Set IIS and AWS date variables
set IISDT=%yyyy:~2,2%%mm%%dd%
set AWSDT=%yyyy%-%mm%-%dd%
The results would look like:
IIS Date: 20100727
AWS Date: 2010-07-27


  1. Thanks man. Your script is very useful.

  2. This script is great. I've been looking around for dos batch yesterday for a while. This rocks!

  3. This is the best way I've found to get yesterday's date in a dos or windows command line or batch file. Great work!!

  4. this is great for echoing date from day before in windows batch files.

  5. Hey i would like get 2 different variables like y1date (which stores 1 day before date) and y2date (which stores 1 day before date). Can u please email me the solution to mayureshmulye@gmail.com
    Thanks in advance.

  6. Great! Thanks!!!!!

  7. Does not work on leap day.. bad.

  8. Great script¡¡¡ works perfect. Thanks

  9. It worked fine for me on leap day this year. I have several scripts that run daily using this and they all worked fine.

  10. Agreed, this is a great script. It's as complex as it needs to be! :)

  11. Thank You, it helps for me too!! :D

  12. This is indeed good, I use it to move the previous days log file to a folder. The only problem is, it doesn't work for moving Friday's log file on Monday. Any way it could be modified to get the previous business days date.

  13. Saved me a good deal of time. Thanks man!

  14. Kudos, much appreciated!

  15. Any idea how this can be amended if I want to find files and copy to folders with this date format:
    DD-MMM-YYYY, e.g

  16. This is perfect!
    Now someone has to do the opposite so we can get the date in the future :)

  17. Thanks so much mate!!

  18. Thanks for providing this, it definitely helped with my batch file problem!

  19. How did they write first operating system if this script only gets you previous day date :-O

  20. This is great. Thanks for sharing this piece of code. It helped a lot in my project.

  21. Bless you! Exactly what I needed.

  22. Great script, works perfectly many thanks.

  23. You made my day! Finally got what I have been looking for. Thank you very very much.

  24. Thank you. I needed.

  25. I'm pretty new to .bat files. I understand that this is supposed to retrieve the previous day's date, but how can I check it to make sure? As soon as I run the .bat file, it closes. Please be gentle lol.

  26. You could add any (or all) of the following to the bottom of the batch file, including "pause." That way it will display the value(s) on screen, then pause the execution of the file.

    echo %IISDT%
    echo %AWSDT%

    1. You are awesome! That is a super simple fix! Thanks*1,000,000!

  27. You're welcome! Glad to help.

  28. Unlike all other scripts that are either uncomplete, confusing, show-off or incorrect, this one really gets to the point and works. many thanks!

  29. Wow does what it says, thanks again

  30. this is great. if you want it to go back to last working day (ie 3 days on Monday) add this at the top
    for /f "skip=1 tokens=1" %%a in ('wmic path win32_localtime get dayofweek ^| findstr /b /r "."') do set /a dow=%%a +1
    for /f "tokens=%dow%" %%a in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do set day=%%a
    echo Today is %dow% which is %day%

    echo if it's Monday we go back 3 days for the filename (friday), otherwise it's just one day
    set dayCnt=1
    if "%dow%"=="2" set dayCnt=3
    echo going back %dayCnt% days

  31. Great script ...very useful

  32. thank every much. you script is useful

  33. super usefull, thanks

  34. Thanks. This is exactly what I was looking for.