Link to home
Start Free TrialLog in
Avatar of hocheeming
hocheeming

asked on

Get Yesterday date in MS DOS Batch file

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%
Avatar of srini_vc
srini_vc

use.

set /a YESTERDAY = DATE - 100


because last 2 bytes used for month, previous to that one is the date field
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%
Avatar of hocheeming

ASKER

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!
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
@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/questions/21275893/File-manipulation-containing-dates-DOS-BATCH-XP.html look in to this for more
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!!
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]"

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