Date Difference Calculation

Just starting out? Need help? Post your questions and find answers here.
tbohon
User
User
Posts: 42
Joined: Sat Nov 22, 2008 4:22 am
Location: Olympia, WA USA

Date Difference Calculation

Post by tbohon »

I need to be able to find and list (eventually to also delete) files of a specific type in a single directory which are more than 3 days old. Program will run automatically each night at 0100 with the intent of keeping the number of files in the directory down to a manageable number.

The following code is, I think, close (it only is supposed to list the files, not delete them at this point) but the calculation of the difference between the current date and the file date isn't working. It lists all files with their last written dates, not just the ones older than 3 days. I'm sure it's something really simple and quite obvious but I just don't see (logically or syntactically) what I'm doing wrong.

Any guidance or comments greatly appreciated.

Thanks in advance.

Tom

Code: Select all

Directory$ = "c:\asmwork"

Today = Date()

Debug "Today's date is " + FormatDate("%yyyy.%mm.%dd", Date())

Debug "Three days ago the date was " + FormatDate("%yyyy.%mm.%dd", AddDate(Date(), #PB_Date_Day, -3)) 

If ExamineDirectory(0, Directory$,"*.asm")
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      Result = GetFileDate(DirectoryEntryName(0), #PB_Date_Modified)
      FDate = AddDate(Date(), #PB_Date_Day, -3)
      If (Result < FDate)
        Debug DirectoryEntryName(0) + " last modified " + FormatDate("%yyyy.%mm.%dd", Result)
      EndIf
    EndIf
  Wend
EndIf
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: Date Difference Calculation

Post by kenmo »

Try changing

Code: Select all

Result = GetFileDate(DirectoryEntryName(0), #PB_Date_Modified)
to

Code: Select all

Result = GetFileDate(Directory$ + DirectoryEntryName(0), #PB_Date_Modified)
because DirectoryEntryName is a relative path (based off whatever directory you examined) but GetFileDate expects a full-path file name (or probably a relative file as long as it's in the program's current directory).

Otherwise GetFileDate will return 0 (not found) which is interpreted as ~40 years old!
tbohon
User
User
Posts: 42
Joined: Sat Nov 22, 2008 4:22 am
Location: Olympia, WA USA

Re: Date Difference Calculation

Post by tbohon »

Kenmo:

I'll certainly give that a try later today.

However, I think my question really is how do I calculate the date difference, i.e., is the code below going to do that or do I need to do something more exotic using the Windows API or a PB trick.

Tnx.

Tom
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Date Difference Calculation

Post by TerryHough »

User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: Date Difference Calculation

Post by kenmo »

Tom, assuming the AddDate function is negative-number-safe, your logic seems fine.

Or you could ignore the AddDate function and simply compare the time difference to a calculated interval:

Code: Select all

SecondsPerDay = 24 * 60 * 60  ; this could be a constant

If (TodaysDate - ModifiedDate) > (3 * SecondsPerDay)
  ; older than three days
Else
  ; less than three days old
EndIf
tbohon
User
User
Posts: 42
Joined: Sat Nov 22, 2008 4:22 am
Location: Olympia, WA USA

Re: Date Difference Calculation

Post by tbohon »

Here is my latest iteration - again close but no cigar:

Code: Select all


Directory$ = "c:\asmwork"

TodaysDate = Date()

SecondsPerDay = 24 * 60 * 60 

Debug "Today's date is " + FormatDate("%yyyy.%mm.%dd", Date())

Debug "Three days ago the date was " + FormatDate("%yyyy.%mm.%dd", AddDate(Date(), #PB_Date_Day, -3)) 

If ExamineDirectory(0, Directory$,"*.asm")
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      ModifiedDate = GetFileDate(DirectoryEntryName(0), #PB_Date_Modified)
      If (TodaysDate - ModifiedDate) > (3 * SecondsPerDay)
        Debug DirectoryEntryName(0) + " last modified " + FormatDate("%yyyy.%mm.%dd", ModifiedDate) + ": OLD FILE!"
      EndIf
    EndIf
  Wend
EndIf

Here is the Debug output - shows all files as being created on 1/1/1970 (base date) when, in fact, helloworld2.asm was modified this evening as part of the test:

Code: Select all

Today's date is 2010.06.03
Three days ago the date was 2010.05.31
hellodialog.asm last modified 1970.01.01: OLD FILE!
helloworld.asm last modified 1970.01.01: OLD FILE!
helloworld1.asm last modified 1970.01.01: OLD FILE!
helloworld2.asm last modified 1970.01.01: OLD FILE!
Module1.asm last modified 1970.01.01: OLD FILE!

Is there some sort of adjustment I need to make in the calculations to account for the 1/1/1970 base date value?

Tnx again in advance.

Tom
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: Date Difference Calculation

Post by kenmo »

Read my first reply again! Then try this:

Code: Select all

Directory$ = "c:\asmwork"

; This guarantees Directory$ always ends in a backslash.
; I suggest using this or something similar to avoid problems later.
;
If Right(Directory$, 1) <> "\"
  Directory$ = Directory$ + "\"
EndIf

TodaysDate = Date()

SecondsPerDay = 24 * 60 * 60

Debug "Today's date is " + FormatDate("%yyyy.%mm.%dd", Date())

Debug "Three days ago the date was " + FormatDate("%yyyy.%mm.%dd", AddDate(Date(), #PB_Date_Day, -3))

If ExamineDirectory(0, Directory$,"*.asm")
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      
      ; You must add Directory$ to the GetFileDate call,
      ; or else it won't find the file (in the program's own folder)
      ; and return 0 (which is 1/1/1970 in Unix time).
      ;
      ModifiedDate = GetFileDate(Directory$ + DirectoryEntryName(0), #PB_Date_Modified)
      If (TodaysDate - ModifiedDate) > (3 * SecondsPerDay)
        Debug DirectoryEntryName(0) + " last modified " + FormatDate("%yyyy.%mm.%dd", ModifiedDate) + ": OLD FILE!"
      EndIf
    EndIf
  Wend
EndIf
tbohon
User
User
Posts: 42
Joined: Sat Nov 22, 2008 4:22 am
Location: Olympia, WA USA

Re: Date Difference Calculation

Post by tbohon »

Kenmo:

OK, I finally understand - your comment about not having the directory prepended to the file name would result in a zero (0) being returned for all 3 dates caused the light bulb to come on.

If I'd just listen to those who know ... :oops:

Anyway,I've learned a LOT from the suggestions here and I truly appreciate the input from everyone on this. I'm going to get my head around PB's way of doing things even if I have to keep studying it after I'm sent to the 'home' ... :D

Thanks again and best wishes.

Tom
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Date Difference Calculation

Post by TerryHough »

This works too. Biggest difference is working with the DirectoryEntry functions rather than using the GetFileDate and having to build and reread the pathname/filename info. It is also a bit more efficient handling the comparisons (less math).

Code: Select all

Directory$ = "c:\asmwork"

ThreeDaysInSeconds = (24 * 60 * 60) * 3
TodaysDate = Date()

Msg$ = "Today's date is " + FormatDate("%yyyy/%mm/%dd %hh:%ii:%ss", TodaysDate) + #LF$
Msg$ + "Three days ago the date was " + FormatDate("%yyyy/%mm/%dd %hh:%ii:%ss", AddDate(TodaysDate, #PB_Date_Day, -3)) +#LF$ + #LF$

If ExamineDirectory(0, Directory$,"*.asm")
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_File
      ModifiedDate = DirectoryEntryDate(0, #PB_Date_Modified)
      If (TodaysDate - ModifiedDate) > ThreeDaysInSeconds
        Msg$ + DirectoryEntryName(0) + #TAB$ +  "modified on " + FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss", ModifiedDate) + #TAB$ + ": OLD FILE!" + #LF$
      EndIf
    EndIf
  Wend
EndIf

MessageRequester("Test",Msg$,#MB_ICONINFORMATION)
Or use a select comparison.

Code: Select all

If ExamineDirectory(0, Directory$,"*.asm")
  While NextDirectoryEntry(0)
    Select DirectoryEntryType(0)
      Case #PB_DirectoryEntry_File
        ModifiedDate = DirectoryEntryDate(0, #PB_Date_Modified)
        Msg$ + DirectoryEntryName(0) + #TAB$ +  "modified on " + FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss", ModifiedDate) + #TAB$ + ": OLD FILE!" + #LF$
    EndSelect
  Wend
EndIf
tbohon
User
User
Posts: 42
Joined: Sat Nov 22, 2008 4:22 am
Location: Olympia, WA USA

Re: Date Difference Calculation

Post by tbohon »

Thought I would share the final product of this thread along with once again thanking everyone for their help. I definitely learned a lot and will continue to learn as I do more and more PB coding.

Oh, and my customer (another analyst at work who has been going in and manually deleting hundreds of files one at a time) is most appreciative for the help as well ... :wink:

Here's the program I ended up giving him:

Code: Select all


     OpenConsole()
     
     PrintN("Program delete5dayold")
     PrintN(" ")
     PrintN("Purging the following .pdf files which are more than 5 days old:")    
     
     Directory$ = GetCurrentDirectory()

     ; This guarantees Directory$ always ends in a backslash.
     ; I suggest using this or something similar to avoid problems later.
     ;
     If Right(Directory$, 1) <> "\"
       Directory$ = Directory$ + "\"
     EndIf

     TodaysDate = Date()

     SecondsPerDay = 24 * 60 * 60

     PrintN("Today's date is " + FormatDate("%yyyy.%mm.%dd", Date()))

     PrintN("Five days ago the date was " + FormatDate("%yyyy.%mm.%dd", AddDate(Date(), #PB_Date_Day, -5)))

     ErrFlag = 0
     
     If ExamineDirectory(0, Directory$,"*.pdf")
       While NextDirectoryEntry(0)
         If DirectoryEntryType(0) = #PB_DirectoryEntry_File

           ; You must add Directory$ to the GetFileDate call,
           ; or else it won't find the file (in the program's own folder)
           ; and return 0 (which is 1/1/1970 in Unix time).
           ;
           ModifiedDate = GetFileDate(Directory$ + DirectoryEntryName(0), #PB_Date_Modified)
           If (TodaysDate - ModifiedDate) > (5 * SecondsPerDay)
             PrintN("Deleting " + DirectoryEntryName(0) + " last modified " + FormatDate("%yyyy.%mm.%dd", ModifiedDate))
             DeleteFile(DirectoryEntryName(0))
           EndIf
         EndIf
       Wend
     EndIf
     
     PrintN(" ")
     PrintN("Operation complete")
     
     
     CloseConsole()

Again, many thanks.

Best,

Tom
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Date Difference Calculation

Post by TerryHough »

Tom, I'm glad you are making progress with this.

But, the code you posted as final will not work as expected.

You've overlooked the necessity to have the complete path & file name in your DeleteFile() command (unless you SetCurrentDirectory(---) to the desired directory). Since you didn't check the results of the DeleteFile() you may not have noticed that it didn't work as you thought.

Try this mod to your code:

Code: Select all

      ModifiedDate = GetFileDate(Directory$ + DirectoryEntryName(0), #PB_Date_Modified)
      If (TodaysDate - ModifiedDate) > (5 * SecondsPerDay)
        ; File should be deleted
        If DeleteFile(Directory$ + DirectoryEntryName(0))
          ; File was successfully deleted
          PrintN("Deleting " + DirectoryEntryName(0) + " last modified " + FormatDate("%yyyy.%mm.%dd", ModifiedDate))
        Else
          ; Deletion failed; incorrect pathname or file was open or marked read only.
          PrintN("Failed to delete " + DirectoryEntryName(0) + " last modified " + FormatDate("%yyyy.%mm.%dd", ModifiedDate))
        EndIf
      EndIf
Post Reply