We help IT Professionals succeed at work.

Get Yesterday date in MS DOS Batch file

hocheeming
hocheeming asked
on
108,597 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%
Comment
Watch Question

Commented:
use.

set /a YESTERDAY = DATE - 100


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

Commented:
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%

Author

Commented:
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!

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

Commented:
@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%"


https://www.experts-exchange.com/Operating_Systems/MSDOS/Q_21275893.html look in to this for more
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
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
Steve you are awesome!!  This was just the code I was looking for too!!!

Commented:
Great Job Steve. This works perfectly!!!
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]"

Commented:
Thanx Steve... Wonderful and easy to use script.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.