Solved calculate a value for yesterday in bat file

July 9, 2011 at 17:30:48
Specs: Windows XP
so far I have:
Set DAY=%Date:~7,2%
Set /a ONEDAYAGO=%DAY-1
ECHO One day ago is %ONEDAYAGO%

except I keep getting -1 instead of 10 when the day value is 11 for example

Any help is appreciated


See More: calculate a value for yesterday in bat file

Report •

#1
July 9, 2011 at 20:28:37
Change this line:
Set /a ONEDAYAGO=%DAY-1

to

Set /a ONEDAYAGO=%DAY%-1

But be aware that using this kind of calculation for dates/times will lead to big problems especially if today is the first day of the month, to get yesterdays date in those circumstances one must calculate what the last day of the previous month was and alter to that. e.g. if today is 01 then yesterday would be any one of 28 29 30 or 31 depending upon the month and whether this is a Leap Year.


Please come back & tell us if your problem is resolved.


Report •

#2
July 9, 2011 at 20:56:56
Thanks for your help. Unfortunately after that change the following was returned:
"Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).

I did consider the 1st of the month issue and have a little routine that corrects for that issue. It does not consider leap year though.

The goal by the way is to rename a file so that yesterday's date is part of the filename - data_2011_07_09.xls for example. There probably is a better way.


Report •

#3
July 9, 2011 at 22:36:14
✔ Best Answer
You can't really trust the date variable, it is settings dependant......

Anyway this seems to be most of the logic:

@echo off
SetLocal EnableDelayedExpansion

rem **** Enter your date substrings here ****
set day=10
set month=07
set year=2011
rem *****************************************

if 1%year% lss 200 set year=20%year%
if not "%day:~1%"=="" if 1%day% lss 110 set day=%day:~1%
if not "%month:~1%"=="" if 1%month% lss 110 set month=%month:~1%

set /a day-=1
IF %day%==0 (
    SET /A month-=1
    IF !month!==0 (
        SET /A year-=1
        SET day=31
        SET month=12
    ) else (
        IF !month!==2 (
            set /a day="28 + (^!(year %% 100) * -1) + ^!(year %% 400) + ^!(year %% 4)"
        ) else (
            FOR %%G IN (1,3,5,7,8,10) DO IF !month!==%%G SET day=31
            FOR %%H IN (4,6,9,11) DO IF !month!==%%H SET day=30
        )
    )
)

IF NOT "%month:~0,1%"=="0" IF %month% LSS 10 SET month=0%month%
IF NOT "%day:~0,1%"=="0" IF %day% LSS 10 SET day=0%day%
echo Day=%day%, year=%year%, month=%month%
pause


Report •

Related Solutions

#4
July 10, 2011 at 02:21:00
Unfortunately after that change the following was returned:
"Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).

You get that message because Set /a attempts to do math on the number 08 or 09 which, because it has a leading zero, is considered to be in octal format but 8 and 9 are invalid values in octal.

Here's a small hybrid VBScript/Batch Script which will give yesterday's date without having to code for months/leap years. You may have to play with the date format to suit your purpose.

set vbs=%temp%\vbs.vbs

> %vbs% echo WScript.Echo DateAdd("d",-1,Date)

for /f "tokens=* delims=" %%a in ('cscript //nologo %vbs%') do (
    set newdate=%%a
)

set newdate=%newdate:~-4%_%newdate:~4,2%_%newdate:~7,2%
echo %newdate%


Please come back & tell us if your problem is resolved.


Report •

Ask Question