Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Yesterday date in MS DOS Batch file

Posted on 2005-02-15
11
Medium Priority
?
90,601 Views
Last Modified: 2013-06-05
Hi,

How do I obtain the yesterday date for MSDOS batch?

I am curently doing the following which is faulty. I am not sure how I can deduce the date correctly. Please advise. Thanks.

CM

--------------------------------------------------------------------------------------------------

for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set DATE=%%d%%c%%b

for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set /a DAY=%%b


set /a YESTERDAY = DATE - 1



echo %DAY%

if  "%DAY%" == "1" (
      set YESTERDAY=%DATE%
)else (
      set YESTERDAY = DATE - 1
)
echo %YESTERDAY%
0
Comment
Question by:hocheeming
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 2

Expert Comment

by:srini_vc
ID: 13321663
use.

set /a YESTERDAY = DATE - 100


because last 2 bytes used for month, previous to that one is the date field
0
 
LVL 2

Expert Comment

by:srini_vc
ID: 13321668
This one would work correctly
-----------------------------------------------------------------------------------

for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set

DATE=%%d%%c%%b

for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set /a

DAY=%%b


set /a YESTERDAY = DATE - 100



echo %DAY%

if  "%DAY%" == "1" (
     set YESTERDAY=%DATE%
)else (
     set YESTERDAY = DATE - 100
)
echo %YESTERDAY%
0
 

Author Comment

by:hocheeming
ID: 13321701
It does not work correctly.

My date format is yyyymmdd.

You cannot just minus off directly as the batch script need to cater for cases like 1st Mar. Which when upon deduction, it should not know that the value is 28 and not 0!
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 2

Expert Comment

by:srini_vc
ID: 13321909
ok. In my system it's in yyyyddmm. that's y i put like that.

The problem u face is some what big. I don't thing 50 points is worth for this
0
 
LVL 2

Expert Comment

by:srini_vc
ID: 13321968
@echo off
setlocal enabledelayedexpansion
:: *** This script runs only on XP
:: *** Get the current date into the variables dd, mm, yy:
for /f "tokens=1-3 delims=/" %%a in ("%date%") do (
  set dd=%%a
  set mm=%%b
  set yy=%%c
)
:: *** Remove leading zeroes for date calculation:
if %dd:~0,1%==0 set dd=%dd:~1,1%
if %mm:~0,1%==0 set mm=%mm:~1,1%
:: *** Create an array with the months' lengths:
set i=1
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
  set Days[!i!]=%%a
  set /a i+=1
)
:: *** (crude) check for a leap year:
set /a Leap=yy %% 4
if %Leap%==0 set Days[2]=29
:: *** Check if today is the first and, if so, calculate overflow:
if %dd%==1 (
  set /a mm -= 1
  if !mm! LSS 1 (
    set /a yy -= 1
    set mm=12
  )
)
if %dd%==1 (
  set dd=!Days[%mm%]!
) else (
  set /a dd -= 1
)
:: *** Add the leading zeroes again:
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
:: *** Put together the logfile's and, while we're at it, the PDF file's name:
set LogFile=log%mm%_%dd%_%yy%.txt
set PDFFile=%yy%_%mm%_%dd%_stats.pdf

echo Name of the log file: %LogFile%
echo Name of the PDF file: %PDFFile%

copy "\stats\%LogFile%" "\devnew\%LogFile%"


http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_21275893.html look in to this for more
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 200 total points
ID: 13324616
Here's a batch file I developed to subtract any number of days from the current date. It accepts a command line parameter of the number of days. The default is 1 day (yesterday):

@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

:CHKDAY

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

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

: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 CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONE

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

echo Date %dayCnt% day(s) before %CurDate% is %mm%/%dd%/%yyyy%

Good Luck,
Steve
0
 

Expert Comment

by:gafeta
ID: 21702180
Thank you SteveGTR for this answer, I have looking for such an answer for more than a month and nobody could do this so simple as you.
By the way, your ranking is really more than fair. ;-)
/Gafeta
0
 

Expert Comment

by:SRG041808
ID: 33992555
Steve you are awesome!!  This was just the code I was looking for too!!!
0
 

Expert Comment

by:knoots
ID: 35088193
Great Job Steve. This works perfectly!!!
0
 

Expert Comment

by:rescapacctgit
ID: 36226843
Hi Steve,

Copied your script into a file called GetYesterday.bat and from command line call it with:

>GetYesterday.bat 07/20/2011 I get back the same date (07/20/2011)

>GetYesterday.bat 2011/07/20 I get back 07/06/2011

>GetYesterday.bat 2011/01/01 I get back 01/16/2006

What am I doing wrong?  Does my OS version matter?   When I type "ver" at the command prompt I get "Microsoft Windows [Version 5.2.3790]"

0
 
LVL 3

Expert Comment

by:Magim_IT
ID: 38035205
Thanx Steve... Wonderful and easy to use script.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question