Computer Hope

Microsoft => Microsoft DOS => Topic started by: BRIANH on September 26, 2008, 11:20:52 AM

Title: date manipulation within a batch file
Post by: BRIANH on September 26, 2008, 11:20:52 AM
Is there something available for date manipulation?  E.G.  I have a file aaa-bbb-20080930   which represents a file created on 20080930.  I execute a process on 20081001 which needs the prior day's file which is  aaa-bbb-20080930 . If I merely subtract 1 from today's date I'm not ending up with what I want in terms of the date part of the file I need to process.

Hopefully there may be something which is non C and Perl....
Title: Re: date manipulation within a batch file
Post by: Dias de verano on September 26, 2008, 04:43:59 PM
search on Google Groups Usenet archive on alt.msdos.batch.nt maybe...
Title: Re: date manipulation within a batch file
Post by: BC_Programmer on September 26, 2008, 07:31:07 PM
Wow, a question Dias doesn't want to tackle... I haven't seen a lot of those in the DOS forum. :X
Title: Re: date manipulation within a batch file
Post by: Dias de verano on September 27, 2008, 01:00:44 AM
(1) No point in reinventing the wheel, and (2) at some point in a budding batch programmer's progress they should be introduced to alt.batch.msdos and its nt variant (3) That's where I get a lot of batch solutions anyway so I thought "Hey - cut out the middle man!" and (4) I was about to open a bottle of wine, organic Argentinian Malbec, and watch a film so I was a bit pressed for time.

However.

For humans it is simple to determine yesterday's date, we just have to think for a moment, but it is tricky to get right in a batch script. For example you need to cater for when today is the first day of the year or month, (How many days in last month; if last month was Feb, is this year a leap year? [Have to implement Zeller's Congruence to crack leap years properly], etc etc. Like the guys in the newsgroup say,

Quote
Getting yesterday's date is simple using a secondary language that has real time functions

so we could for example create a one line vbs script which discovers yesterday's date and echoes it

Code: [Select]
wscript.echo (Date()- 1)
and then call it from a batch file. It is probably simplest to create the vbs script within the batch file using echo and redirection, taking care to escape the parenthesis characters.

I also see from the date format used in BRIANH's question that his local date format appears to be the US style, i.e. mm/dd/yyyy so I will assume that.

Having got yesterday's date back from the vbscript it is necessary to remove the slashes and rearrange the digits to obtain the yyyymmdd format seen in the filename datestamp examples shown.

Code: [Select]
@echo off
echo wscript.echo ^(Date^(^)- 1^)>yesterday.vbs
for /f %%a in ('cscript //nologo yesterday.vbs') do set ydate1=%%a
del yesterday.vbs
set ydate1=%ydate1:/=%
set m=%ydate1:~0,2%
set d=%ydate1:~2,2%
set y=%ydate1:~4,4%
set ydate2=%y%%m%%d%
echo yesterday was %ydate2%
Title: Re: date manipulation within a batch file
Post by: BC_Programmer on September 27, 2008, 11:27:06 AM
I wasn't saying you couldn't solve the problem- I thought you had simply- or, I should say- finally, lost patience to give it any of your time.
Title: Re: date manipulation within a batch file
Post by: Dias de verano on September 27, 2008, 12:08:10 PM
I wasn't saying you couldn't solve the problem- I thought you had simply- or, I should say- finally, lost patience to give it any of your time.

Oh ye of little faith....   ;)
Title: Re: date manipulation within a batch file
Post by: Hedonist on September 27, 2008, 05:03:46 PM
I modified the code to suit my date format which is yyyy-mm-dd and it works fine except that if the current date is 2008-04-01 the new date is 2008-03-31 instead of 2008-03-29, where have I gone wrong please?

Amended code
Code: [Select]
@echo off
cls

echo wscript.echo ^(Date^(^)- 1^)>yesterday.vbs
for /f %%a in ('cscript //nologo yesterday.vbs') do set ydate1=%%a
del yesterday.vbs

set m=%ydate1:~5,2%
set d=%ydate1:~-2%
set y=%ydate1:~0,4%

set ydate2=%y%-%m%-%d%
echo yesterday was %ydate2%

See below for how to make an absolute chump of oneself :-[ :'(
Title: Re: date manipulation within a batch file
Post by: Dias de verano on September 27, 2008, 05:40:11 PM
I modified the code to suit my date format which is yyyy-mm-dd and it works fine except that if the current date is 2008-04-01 the new date is 2008-03-31 instead of 2008-03-29, where have I gone wrong please?

The code is working fine. I suspect that "where you have gone wrong" is in confusing March and February. If "today" is the first day of the fourth month, that is, the first of April, then "yesterday" is the last day of March, which has thirty-one days (every year).

If the current date were 2008-03-01 (The first of March, the third month of the year) then yesterday, since 2008 is a leap year, would be the twenty-ninth day of February, 2008-02-29.

Here is a little rhyme which, if you learn it, may help you avoid this (evidently) tricky problem...

    Thirty days hath September,
    April, June, and November:
    All the rest have thirty-one,
    Except for February
    Which hath but twenty-eight days clear,
    And twenty-nine in each leap year.



Title: Re: date manipulation within a batch file
Post by: Hedonist on September 27, 2008, 06:14:18 PM
Quote
I suspect that "where you have gone wrong" is in confusing March and February.

No, 'twas nowt to do with that.  I have discovered that when I awoke this a.m. I failed to switch on my brain before allowing fingers on keyboard.

Thanks Dias, as usual a fine piece of coding.