Author Topic: Using Win32 API functions to see date when file was created, accessed, modified.  (Read 3047 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Playing around with (and learning) Win32 API functions some more.  Here's a way to retrieve a files created data, and modified and accessed date.  Not 100% sure I'm doing it right, but in my testing here it seems to work when comparing results to what my windows file managers reports on the files.  I included the SetFileTime function in the code too, but I didn't use it here.

WINDOWS ONLY CODE.

- Dav

Code: QB64: [Select]
  1. '============
  2. 'FILETIME.BAS (FOR WINDOWS ONLY)
  3. '============
  4. 'Win32 API fucntions to get/set time info of a file.
  5. 'Shows when file was created, modified, accessed.
  6. 'QB64 code by Dav, AUG/2020
  7.  
  8. 'All API info found on this page:
  9. 'http://allapi.mentalis.org/apilist/apilist.php
  10.  
  11. CONST OPEN_EXISTING = 3
  12.  
  13. TYPE FILETIME
  14.     dwLowDateTime AS LONG
  15.     dwHighDateTime AS LONG
  16.  
  17. TYPE SYSTEMTIME
  18.     wYear AS INTEGER
  19.     wMonth AS INTEGER
  20.     wDayOfWeek AS INTEGER
  21.     wDay AS INTEGER
  22.     wHour AS INTEGER
  23.     wMinute AS INTEGER
  24.     wSecond AS INTEGER
  25.     wMilliseconds AS INTEGER
  26.  
  27.     FUNCTION GetFileTime& (BYVAL hFile&, lpCreationTime AS FILETIME, lpLastAccessTime AS FILETIME, lpLastWriteTime AS FILETIME)
  28.     FUNCTION SetFileTime& (BYVAL hFile&, lpCreationTime AS FILETIME, lpLastAccessTime AS FILETIME, lpLastWriteTime AS FILETIME)
  29.     FUNCTION CreateFileA& (lpFileName$, BYVAL dwDesiredAccess&, BYVAL dwShareMode&, BYVAL lpSecurityAttributes&, BYVAL dwCreationDisposition&, BYVAL dwFlagsAndAttributes&, BYVAL hTemplateFile&)
  30.     FUNCTION CloseHandle& (BYVAL hObject AS LONG)
  31.     FUNCTION FileTimeToSystemTime& (lpFileTime AS FILETIME, lpSystemTime AS SYSTEMTIME)
  32.     FUNCTION FileTimeToLocalFileTime& (lpFileTime AS FILETIME, lpLocalFileTime AS FILETIME)
  33.  
  34. DIM F1 AS FILETIME, F2 AS FILETIME, F3 AS FILETIME, SysTime AS SYSTEMTIME
  35.  
  36.  
  37. fil$ = "qb64.exe" 'use qb64.exe for a test
  38. PRINT "Results for file: "; fil$
  39. PRINT "----------------"
  40.  
  41. 'create file handle (using open as existing, so not creating a file)
  42. hFile = CreateFileA(fil$, 0, 0, 0, OPEN_EXISTING, 0, 0)
  43.  
  44. 'Get file's time info
  45. x& = GetFileTime(hFile, F1, F2, F3)
  46.  
  47. IF x& = -1 THEN
  48.     PRINT "Invalid handle error. File not opened.": END
  49.  
  50. 'See when file was created.....
  51. x& = FileTimeToLocalFileTime(F1, F1) 'Convert file time to local file time
  52. x& = FileTimeToSystemTime(F1, SysTime) 'Convert file time to system file time
  53. PRINT "Created : "; SysTime.wMonth; "/"; SysTime.wDay; "/"; SysTime.wYear
  54.  
  55. 'See when file was last accessed.....
  56. x& = FileTimeToLocalFileTime(F2, F1) 'Convert file time to local file time
  57. x& = FileTimeToSystemTime(F1, SysTime) 'Convert file time to system file time
  58. PRINT "Accessed: "; SysTime.wMonth; "/"; SysTime.wDay; "/"; SysTime.wYear
  59.  
  60. 'See when file was last modified.....
  61. x& = FileTimeToLocalFileTime(F3, F1) 'Convert file time to local file time
  62. x& = FileTimeToSystemTime(F1, SysTime) 'Convert file time to system file time
  63. PRINT "Modified: "; SysTime.wMonth; "/"; SysTime.wDay; "/"; SysTime.wYear
  64.  
  65. 'close file handle
  66. x& = CloseHandle(hFile)
  67.  
  68.  


Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Sorry, Looks like this is already in the wiki under libraries - and showing a better example. You can delete this post if you want.

- Dav