[Solved] How to check time and date on a file

General FreeBASIC programming questions.
Gablea
Posts: 1049
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

[Solved] How to check time and date on a file

Postby Gablea » Dec 16, 2016 20:36

Hi everyone,
Can I ask if what j want to do is possible.

What I want is to check a file's creation date and time to see if they have changed.

Example created 12/12/2016 time 12:30 and then if the file is recreated it would be like 12/12/2016 12:35 etc

Would this work on dos and Linux as well as Windows.
Last edited by Gablea on Feb 14, 2019 8:34, edited 1 time in total.
MrSwiss
Posts: 3220
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to check time and date on a file

Postby MrSwiss » Dec 16, 2016 21:15

Hi Gablea,

for Info's on DOS and Windows see: MSDN. DOS/WIN are already different!
No idea, how 'ix systems handle that sort of thing.

Generally: it seems to be closely related to the used File System ...
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to check time and date on a file

Postby fxm » Dec 16, 2016 21:46

See keyword: FileDateTime
MrSwiss
Posts: 3220
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to check time and date on a file

Postby MrSwiss » Dec 16, 2016 22:08

Hi fxm,

can you explain, which of the many Date/Time (that a WIN-File holds) is returned by FileDateTime?
fxm
Posts: 9126
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to check time and date on a file

Postby fxm » Dec 16, 2016 22:21

As described in documentation:
"File last modified date"
PaulSquires
Posts: 757
Joined: Jul 14, 2005 23:41
Contact:

Re: How to check time and date on a file

Postby PaulSquires » Dec 16, 2016 23:49

Jose Roca has these functions (and many many more) in his AfxWin.inc source code files. You can get them all from the "CWindow" package over on http://www.planetsquires.com/protect/forum/index.php

Code: Select all


' ========================================================================================
' Returns the time the file was created.
' - wszFileSpec: The directory or path, and the file name, which can include wildcard characters,
'   for example, an asterisk (*) or a question mark (?).
'   This parameter should not be NULL, an invalid string (for example, an empty string or a
'   string that is missing the terminating null character), or end in a trailing backslash (\).
'   If the string ends with a wildcard, period (.), or directory name, the user must have access
'   permissions to the root and all subdirectories on the path.
'   To extend the limit from MAX_PATH to 32,767 wide characters, prepend "\\?\" to the path.
' - bUTC: Pass FALSE if you want to get the time in local time (the NTFS file system stores time
'   values in UTC format, so they are not affected by changes in time zone or daylight saving time).
'   FileTimeToLocalFileTime uses the current settings for the time zone and daylight saving time.
'   Therefore, if it is daylight saving time, it takes daylight saving time into account, even
'   if the file time you are converting is in standard time.
' Usage: AfxFileTimeToDateStr(AfxGetFileCreationTime("C:\Tests\test.bas", FALSE), "dd/MM/yyyy")
' ========================================================================================
PRIVATE FUNCTION AfxGetFileCreationTime (BYREF wszFileSpec AS WSTRING, BYVAL bUTC AS BOOLEAN = TRUE) AS FILETIME
   DIM fd AS WIN32_FIND_DATAW
   DIM hFind AS HANDLE = FindFirstFileW(wszFileSpec, @fd)
   IF hFind <> INVALID_HANDLE_VALUE THEN
      FindClose hFind
      IF bUTC = TRUE THEN
         RETURN fd.ftCreationTime
      ELSE
         DIM FT AS FILETIME
         FileTimeToLocalFileTime(@fd.ftCreationTime, @FT)
         RETURN FT
      END IF
   END IF
END FUNCTION
' ========================================================================================

' ========================================================================================
' Returns the time the file was last accessed. The NTFS file system delays updates to the
' last access time for a file by up to 1 hour after the last access.
' ========================================================================================
PRIVATE FUNCTION AfxGetFileLastAccessTime (BYREF wszFileSpec AS WSTRING, BYVAL bUTC AS BOOLEAN = TRUE) AS FILETIME
   DIM fd AS WIN32_FIND_DATAW
   DIM hFind AS HANDLE = FindFirstFileW(wszFileSpec, @fd)
   IF hFind <> INVALID_HANDLE_VALUE THEN
      FindClose hFind
      IF bUTC = TRUE THEN
         RETURN fd.ftLastAccessTime
      ELSE
         DIM FT AS FILETIME
         FileTimeToLocalFileTime(@fd.ftLastAccessTime, @FT)
         RETURN FT
      END IF
   END IF
END FUNCTION
' ========================================================================================

' ========================================================================================
' Returns the time the file was last written to, truncated, or overwritten.
' When writing to a file, the last write time is not fully updated until all handles that
' are used for writing are closed.
' ========================================================================================
PRIVATE FUNCTION AfxGetFileLastWriteTime (BYREF wszFileSpec AS WSTRING, BYVAL bUTC AS BOOLEAN = TRUE) AS FILETIME
   DIM fd AS WIN32_FIND_DATAW
   DIM hFind AS HANDLE = FindFirstFileW(wszFileSpec, @fd)
   IF hFind <> INVALID_HANDLE_VALUE THEN
      FindClose hFind
      IF bUTC = TRUE THEN
         RETURN fd.ftLastWriteTime
      ELSE
         DIM FT AS FILETIME
         FileTimeToLocalFileTime(@fd.ftLastWriteTime, @FT)
         RETURN FT
      END IF
   END IF
END FUNCTION
' ========================================================================================
Gablea
Posts: 1049
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: How to check time and date on a file

Postby Gablea » Dec 17, 2016 0:06

But does that function work under the other supported operating systems?
PaulSquires
Posts: 757
Joined: Jul 14, 2005 23:41
Contact:

Re: How to check time and date on a file

Postby PaulSquires » Dec 17, 2016 0:35

Gablea wrote:But does that function work under the other supported operating systems?

Nope, Windows only because the functions use the WinAPI. I would be very surprised if you can get a one size fits all type of code to cover all DOS, Windows and Linux.
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to check time and date on a file

Postby dodicat » Dec 17, 2016 3:57

Going by fxm's help link, and using a format used in the U.K.

Code: Select all

#include "vbcompat.bi"

Dim filename As String


filename="Brand new blank file.txt"

sub getstamp(filename as string)
If FileExists( filename ) Then
    print filename;":"
  Print "File last modified: ";
  dim as double d = FileDateTime( filename )
  Print Format( d, "dd-mm-yyyy hh:mm:ss " )'rub out the :ss if you wish
Else
  Print filename;" not found"
End If
end sub

' ===== optional to test =====
if filename="Brand new blank file.txt" then
dim as long f=freefile
open filename for output as #f
close #f
end if
'============================


getstamp(filename)'or any other file you choose

if filename="Brand new blank file.txt" then kill filename

sleep
St_W
Posts: 1470
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: How to check time and date on a file

Postby St_W » Dec 17, 2016 22:40

PaulSquires wrote:I would be very surprised if you can get a one size fits all type of code to cover all DOS, Windows and Linux.

If you use the functionality from the C runtime you can do that. However, for some reason the CRT headers seem to be not okay (at least for fbc 64-bit Windows) so I've included the necessary definitions in the code. I've written and tested this on Windows, but it should also work on Linux and maybe also on DOS.

Code: Select all

'#Include "crt.bi"
'#include "crt/io.bi"
'#include "crt/sys/stat.bi"
#include "string.bi"

extern "C"
   Type time_t As Integer
   Type size_t As UInteger
   type tm
      tm_sec as long
      tm_min as long
      tm_hour as long
      tm_mday as long
      tm_mon as long
      tm_year as long
      tm_wday as long
      tm_yday as long
      tm_isdst as long
   end type
   Type _stat
      st_dev as ULong
      st_ino as UShort
      st_mode as UShort
      st_nlink as short
      st_uid as short
      st_gid as short
      st_rdev as ULong
      st_size as ULong
      st_atime as time_t
      st_mtime as time_t
      st_ctime as time_t
   end Type
   Declare function stat (byval as zstring ptr, byval as _stat ptr) as Long
   declare function localtime (byval as time_t ptr) as tm ptr
   declare function strftime (byval as zstring ptr, byval as size_t, byval as zstring ptr, byval as tm ptr) as size_t
End extern


Function format_time(t As tm ptr) As String
   Return format(1900+t->tm_year, "0000") + "-" + format(1+t->tm_mon, "00") + "-" + format(t->tm_mday, "00") + " " + format(t->tm_hour, "00") + ":" + format(t->tm_min, "00") + ":" + format(t->tm_sec, "00")
End Function



Dim finfo As _stat
Dim tinfo As tm Ptr
dim result As Integer

result = stat(@"test.bin", @finfo)


If result <> 0 Then
   Print "Error getting file stats"
Else
   tinfo = localtime(@finfo.st_mtime)
   Print "Modification time: " + format_time(tinfo)
EndIf


Sleep


See also
http://freebasic.net/wiki/wikka.php?wak ... time#FILES
cerveza
Posts: 8
Joined: Dec 27, 2013 21:07

Re: How to check time and date on a file

Postby cerveza » Jul 09, 2017 3:50

It would be ugly but get the job done.
Shell out with a

dir > temp.txt
or
ls -la > temp.txt

Then look thru the txt file for your file name and check the date next to it.
srvaldez
Posts: 2063
Joined: Sep 25, 2005 21:54

Re: How to check time and date on a file

Postby srvaldez » Jul 09, 2017 12:20

Gablea wrote:But does that function work under the other supported operating systems?

the FileDateTime example pointed to by fxm works on OS X and my guess is that it should work on linux as well.
St_W solution does not work on OS X, it gives wrong info.
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to check time and date on a file

Postby dodicat » Jul 09, 2017 12:41

Using cerveas's idea above for Windows 10:

Code: Select all




function datetime(file as string) as string
 shell "dir "& file & ">tempdirfile.txt"
 dim as string f
 var  n=freefile
    Open "tempdirfile.txt" For Binary Access Read As #n
    If Lof(1) > 0 Then
      f = String(Lof(n), 0)
      Get #n, , f
    End If
    Close #n
    var ir=instrrev(file,"\")+1
    file=mid(file,ir)
 var i= instr(f,file)-36
 var t= mid(f,i,17)
 function= file & "   was created:  " &t
 kill "tempdirfile.txt"
end function


var f="C:\Windows\explorer.exe"


print datetime(f)
sleep
 
St_W
Posts: 1470
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: How to check time and date on a file

Postby St_W » Jul 09, 2017 23:46

srvaldez wrote:
Gablea wrote:But does that function work under the other supported operating systems?
St_W solution does not work on OS X, it gives wrong info.
It should work on (nearly) any operating system. However it depends on the "stat" structure, which is obviously different on different operating systems. My definition of the "stat" structure in the code above is only valid for Windows, that is why the code does not work on Linux/OSX. FreeBasic's CRT headers should be fixed, then this would work out of the box. I'll probably have a look on this at some time in the future, but can't make any promises yet.
TeeEmCee
Posts: 259
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: How to check time and date on a file

Postby TeeEmCee » Jul 10, 2017 2:28

What's wrong with using FB's builtin FileDateTime function? It works on all OSes and it's much easier.

St_W wrote:It should work on (nearly) any operating system. However it depends on the "stat" structure, which is obviously different on different operating systems. My definition of the "stat" structure in the code above is only valid for Windows, that is why the code does not work on Linux/OSX. FreeBasic's CRT headers should be fixed, then this would work out of the box. I'll probably have a look on this at some time in the future, but can't make any promises yet.

FB's crt headers are mostly wrong (or nonexistent) on all platforms except Linux and Windows. They should be mostly-correct for Linux -- but after reading your post I looked, and was surprised to see that 'stat' is only defined on win32!
I started creating proper crt headers for FreeBSD (plus just stdio.bi for OSX). Updating them is pretty tricky, because they share some common include files, sometimes include each other, and the original C headers are organised differently on each OS; it's a bit of a mess.
If you are interested in updating the headers, see what dkl wrote here about doing it automatically (which unfortunately didn't work out).

Note that stat isn't part of the C standard library; it's part of POSIX (and provided on win32 too). I recommend writing C code instead of FB (and linking it to your FB program) when you want to write portable code that accesses functions in Unix system headers, because the FB headers just can't be trusted. There's heaps of complexity in the original headers (e.g. ARM vs x86) that's been stripped out in translation. Accessing the winapi from FB works fine though.

Return to “General”

Who is online

Users browsing this forum: No registered users and 26 guests