[Solved] How to check time and date on a file
[Solved] How to check time and date on a file
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.
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.
Re: How to check time and date on a file
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 ...
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 ...
Re: How to check time and date on a file
See keyword: FileDateTime
Re: How to check time and date on a file
Hi fxm,
can you explain, which of the many Date/Time (that a WIN-File holds) is returned by FileDateTime?
can you explain, which of the many Date/Time (that a WIN-File holds) is returned by FileDateTime?
Re: How to check time and date on a file
As described in documentation:
"File last modified date"
"File last modified date"
-
- Posts: 1002
- Joined: Jul 14, 2005 23:41
Re: How to check time and date on a file
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
' ========================================================================================
Re: How to check time and date on a file
But does that function work under the other supported operating systems?
-
- Posts: 1002
- Joined: Jul 14, 2005 23:41
Re: How to check time and date on a file
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.Gablea wrote:But does that function work under the other supported operating systems?
Re: How to check time and date on a file
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
Re: How to check time and date on a file
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.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.
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
http://freebasic.net/wiki/wikka.php?wak ... time#FILES
Re: How to check time and date on a file
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.
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.
Re: How to check time and date on a file
the FileDateTime example pointed to by fxm works on OS X and my guess is that it should work on linux as well.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.
Re: How to check time and date on a file
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
Re: How to check time and date on a file
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.srvaldez wrote:St_W solution does not work on OS X, it gives wrong info.Gablea wrote:But does that function work under the other supported operating systems?
Re: How to check time and date on a file
What's wrong with using FB's builtin FileDateTime function? It works on all OSes and it's much easier.
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.
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!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.
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.