Problem with GetFileDate() [SOLVED]

Just starting out? Need help? Post your questions and find answers here.
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Problem with GetFileDate() [SOLVED]

Post by bmcs »

I am trying to learn how to use PB's file system commands.
Using the help file example FileSystem.pb I have added a simple Else statement:

Code: Select all

FileName$ = DirectoryEntryName(0)
If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
	FileName$ = "[DIR] " + FileName$
Else
	; D$ is not rquired, only added for clarity
	D$ = FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate(FileName$, #PB_Date_Modified))
	FileName$ = FileName$ + " - " + D$
EndIf
In every case the date string defaults to the base 1970 date and time.
Obviously there is not a bug in GetFileDate(), so can some kind person show me what I am doing wrong?
TIA
Dave
Win 7 x64 & PB 5.60
Last edited by bmcs on Mon Jun 05, 2017 2:04 pm, edited 1 time in total.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Problem with GetFileDate()

Post by kenmo »

GetFileDate() returns 0 for a missing file or wrong path:

Code: Select all

Debug FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate("missing", #PB_Date_Modified))
You're passing a relative file path (just its name), so GetFileDate() is looking in the program's current directory.
Something like this would probably fix your issue:

Code: Select all

   FullPath$ = ExaminedDir$ + FileName$
   D$ = FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate(FullPath$, #PB_Date_Modified))
   FileName$ = FileName$ + " - " + D$
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Re: Problem with GetFileDate()

Post by bmcs »

kenmo wrote:GetFileDate() returns 0 for a missing file or wrong path:

Code: Select all

Debug FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate("missing", #PB_Date_Modified))
OK! That explains the 1970 (0) result, but not the reason.
You're passing a relative file path (just its name), so GetFileDate() is looking in the program's current directory.
Something like this would probably fix your issue:

Code: Select all

   FullPath$ = ExaminedDir$ + FileName$
   D$ = FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate(FullPath$, #PB_Date_Modified))
   FileName$ = FileName$ + " - " + D$
Looking through the help file example FileSystem.pb I still don't understand why after a previous call to ExamineDirectory() to get the FileName$ it would be necessary to call ExamineDirectory() again. Seems like a highly convoluted mechanism. I was hoping to make something cross platform, but I think I will go with the Win API for now.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Problem with GetFileDate()

Post by kenmo »

No, you don't need to call ExamineDirectory() again, I meant to show you need to add the full path to the filename. No API needed either.

GetFileDate() is not based on ExamineDirectory(), so it doesn't know which folder you're looking at, so you usually need to specify a full path.

BUT, when I replied, I forgot about DirectoryEntryDate(), a similar function which IS based on ExamineDirectory():

Code: Select all

ExaminedDir$ = GetHomeDirectory()
If ExamineDirectory(0, ExaminedDir$, "")
  While NextDirectoryEntry(0)
    FileName$ = DirectoryEntryName(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
       ;Debug "[DIR] " + FileName$
    Else
       ; D$ is not rquired, only added for clarity
       
       ; bad
       Debug FileName$ + " " + FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate(FileName$, #PB_Date_Modified))
       
       ; good
       Debug FileName$ + " " + FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", GetFileDate(ExaminedDir$ + FileName$, #PB_Date_Modified))
       
       ; good
       Debug FileName$ + " " + FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", DirectoryEntryDate(0, #PB_Date_Modified))
       Debug ""
       
    EndIf
  Wend
  FinishDirectory(0)
EndIf
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Re: Problem with GetFileDate() [SOLVED]

Post by bmcs »

Thanks Kenmo, that clarified the whole issue for me.
Dave
Post Reply