Page 1 of 1
Problem with GetFileDate() [SOLVED]
Posted: Mon Jun 05, 2017 12:30 pm
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
Re: Problem with GetFileDate()
Posted: Mon Jun 05, 2017 1:03 pm
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$
Re: Problem with GetFileDate()
Posted: Mon Jun 05, 2017 1:36 pm
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.
Re: Problem with GetFileDate()
Posted: Mon Jun 05, 2017 1:52 pm
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
Re: Problem with GetFileDate() [SOLVED]
Posted: Mon Jun 05, 2017 2:07 pm
by bmcs
Thanks Kenmo, that clarified the whole issue for me.
Dave