Ooops
January 20, 2021, 01:35:55 PM

Author Topic: [bb] File Date/Time/Attr functions for b+ by Snarkbait [ 1+ years ago ]  (Read 519 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : File Date/Time/Attr functions for b+
Author : Snarkbait
Posted : 1+ years ago

Description : Uses kernel32 functions to get file date/time information, also you can get file attribute information.

GetFileDateStringFromType() function returns date in US format, apologies to those from elsewhere... could easily be altered to your local accepted date/time format.


Code :
Code: BlitzBasic
  1. ; filetime.bb include file
  2. ; File Date & Time, Attributes functions
  3. ; by snarkbait
  4. ; snarkbait66@gmail.com
  5. ;
  6. ; typical Microsoft - it takes no less than FIVE api function calls to get the proper date!
  7. ;
  8. ; add the following to kernel32.decls
  9. ; .lib "kernel32.dll"
  10. ;
  11. ;api_GetFileTime%(hFile%,lpCreationTime*,lpLastAccessTime*,lpLastWriteTime*):"GetFileTime"
  12. ;api_CreateFile%(FileName$, dwDesiredAccess%, dwShareMode%, lpSecurity%, dwCreationDsp%, dwFlagsAndAttr%, hTemplate%):"CreateFileA"
  13. ;api_FileTimeToSystemTime%(lpFileTime*, lpSystemTime*):"FileTimeToSystemTime"
  14. ;api_CloseHandle%( hObject% ):"CloseHandle"
  15. ;api_FileTimeToLocalFileTime%(lpFileTime*, lpLocalFileTime*):"FileTimeToLocalFileTime"
  16. ;
  17. ;; also, to get file attributes, add
  18. ; api_GetFileAttributes%( lpFilename$ ):"GetFileAttributesA"
  19. ;
  20.  
  21.  
  22. Type SYSTEMTIME
  23.         Field wYear%
  24.         Field wMonth%
  25.         Field wDayOfWeek%
  26.         Field wDay%
  27.         Field wHour%
  28.         Field wMinute%
  29.         Field wSecond%
  30.         Field wMilliseconds%
  31. End Type
  32.  
  33. ; API constants
  34.  
  35. ; CreateFile:Desired Access
  36. Const GENERIC_WRITE = $40000000
  37. Const GENERIC_READ = $80000000
  38. ; CreateFile:FlagsAndAttributes
  39. Const FILE_ATTRIBUTE_NORMAL = $80
  40. ; CreateFile:CreationDisposition
  41. Const OPEN_EXISTING = 3
  42. ; CreateFile:ShareMode
  43. Const FILE_SHARE_READ = 1
  44. Const FILE_SHARE_WRITE = 2
  45.  
  46. ; other constants
  47. Const FILETIMESIZE = 8
  48. Const SYSTEMTIMESIZE = 16
  49. Const GET_CREATION = 0
  50. Const GET_LASTACCESS = 1
  51. Const GET_LASTMODIFIED = 2
  52. Const ATTR_STRING$ = "RHSVDALC"
  53.  
  54. ;==========================================================================
  55. ; FUNCTION: FileDateTime()
  56. ; PARAMS:
  57. ;                       filename$ : valid filename/path string
  58. ;                       [returnType] : default GET_LASTMODIFIED, or GET_CREATION, GET_LASTACCESS
  59. ;                                               Specifies if you want the file creation, last modified, or last accessed date/time
  60. ;                                               Last Modified is the generally used 'file date'
  61. ; CALLS:
  62. ;                       FileExists()
  63. ;                       api_CreateFile() : kernel32.dll function - returns handle for subsequent i/o ops
  64. ;                       api_GetFileTime() : kernel32.dll function - fills passed banks with 64-bit UTC date/time value
  65. ;                       api_FileTimeToLocalTime : kernel32.dll function - converts from 64bit UTC to systemtime struct
  66. ;                       api_FileTimeToSystemTime : kernel32.dll function - converts sytemtime struct to same with localized date/time values
  67. ;                       api_CloseHandle : kernel32.dll - Closes file handle opened with api_CreateFile()
  68. ;                       GetFileDateFromBank() : constructor method for type SYSTEMTIME, creates instance and
  69. ;                                                                       fills it with values from the passed bank
  70. ;
  71. ; RETURNS:
  72. ;       Instance of type SYSTEMTIME
  73. ; OTHER: If you do not specify dwShareMode for CreateFile, and filename is already opened, WILL CAUSE CRASH.
  74. ;               Keep sharemode to 'FILE_SHARE_READ Or FILE_SHARE_WRITE'
  75. Function FileDateTime.SYSTEMTIME( filename$, returnType = GET_LASTMODIFIED )
  76.         Local hFile%
  77.         Local success%
  78.         Local retValue%
  79.         Local bankCreationFileTime = CreateBank(FILETIMESIZE)
  80.         Local bankLastAccessFileTime = CreateBank(FILETIMESIZE)
  81.         Local bankLastModifiedFileTime = CreateBank(FILETIMESIZE)
  82.         Local bankLocalFileTime = CreateBank(FILETIMESIZE)
  83.         Local bankSystemTime = CreateBank(SYSTEMTIMESIZE)
  84.        
  85.         ; check file
  86.         If Not FileExists( filename$ )
  87.                 Return Null
  88.         EndIf
  89.        
  90.         ; get api file handle
  91.         hFile = api_CreateFile( filename$, GENERIC_WRITE Or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  92.         If hFile > 0
  93.                 ; get 64-bit UDT, for all three options
  94.                 success = api_GetFileTime(hFile, bankCreationFileTime, bankLastAccessFileTime, bankLastModifiedFileTime)
  95.                 If success
  96.                         ; convert selected return type to local time
  97.                         Select returnType
  98.                                 Case GET_CREATION
  99.                                         success = api_FileTimeToLocalFileTime( bankCreationFileTime, bankLocalFileTime )
  100.                                 Case GET_LASTACCESS
  101.                                         success = api_FileTimeToLocalFileTime( bankLastAccessFileTime, bankLocalFileTime )
  102.                                 Case GET_LASTMODIFIED
  103.                                         success = api_FileTimeToLocalFileTime( bankLastModifiedFileTime, bankLocalFileTime )
  104.                                 Default
  105.                                         success = False
  106.                         End Select
  107.                         If success
  108.                                 ; convert to structured time info ( & free unused banks)
  109.                                 FreeBank bankCreationFileTime
  110.                                 FreeBank bankLastModifiedFileTime
  111.                                 FreeBank bankLastAccessFileTime
  112.                                 success = api_FileTimeToSystemTime(bankLocalFileTime, bankSystemTime)
  113.                                 If success
  114.                                         ; create type object and fill with bank values
  115.                                         FreeBank bankLocalFileTime
  116.                                         this.SYSTEMTIME = GetFileDateFromBank( bankSystemTime )
  117.                                         FreeBank bankSystemTime
  118.                                         ; didn't create type properly
  119.                                         If this.SYSTEMTIME = Null
  120.                                                 retValue = False
  121.                                         Else
  122.                                                 ; everything OK
  123.                                                 retValue = True
  124.                                         EndIf
  125.                                 Else
  126.                                         ; error, but needs close file
  127.                                         retValue = False
  128.                                 EndIf
  129.                         Else
  130.                                 ; error, but needs close file
  131.                                 retValue = False
  132.                         EndIf
  133.                 Else
  134.                         ; error, but needs close file
  135.                         retValue = False
  136.                 EndIf
  137.         Else
  138.                 ; error
  139.                 Return Null
  140.         EndIf
  141.        
  142.         If retValue
  143.                 ; everything OK, close file and return Object
  144.                 api_CloseHandle( hFile)
  145.                 Return this
  146.         Else
  147.                 ; SNAFU, close file and return null
  148.                 api_CloseHandle( hFile)
  149.                 Return Null
  150.         EndIf
  151. End Function
  152.  
  153. ;==========================================================================
  154. ; FUNCTION: GetFileDateFromBank()
  155. ; PARAMS:
  156. ;                       inBank : 16-byte bank in the SYSTEMTIME format
  157. ; CALLS:
  158. ; RETURNS:
  159. ;       Instance of type SYSTEMTIME, filled with values from bank
  160. ; OTHER:
  161. ;
  162.  
  163. Function GetFileDateFromBank.SYSTEMTIME( inBank )
  164.  
  165.         ; check bank is the right size
  166.         If BankSize( inBank ) < SYSTEMTIMESIZE Return Null
  167.        
  168.         ; create instance
  169.         Local this.SYSTEMTIME = New SYSTEMTIME
  170.        
  171.         ; peek 2-byte values from bank
  172.         thiswYear = PeekShort( inBank, 0 )
  173.         thiswMonth = PeekShort( inBank, 2 )
  174.         thiswDayOfWeek = PeekShort( inBank, 4 )
  175.         thiswDay = PeekShort( inBank, 6 )
  176.         thiswHour = PeekShort( inBank, 8 )
  177.         thiswMinute = PeekShort( inBank, 10 )
  178.         thiswSecond = PeekShort( inBank, 12 )
  179.         thiswMilliseconds = PeekShort( inBank, 14 )
  180.        
  181.         ; return Object
  182.         Return this
  183. End Function
  184.  
  185. ;==========================================================================
  186. ; FUNCTION: GetFileDateStringFromType()
  187. ; PARAMS:
  188. ;                       this.SYSTEMTIME : object of type SYSTEMTIME
  189. ; CALLS:
  190. ; RETURNS:
  191. ;       String in American date format, i.e. "MM/DD/YYYY HH:MM:SS {AM/PM}"
  192. ; OTHER:               
  193. ;
  194.  
  195. Function GetFileDateStringFromType$( this.SYSTEMTIME )
  196.         Local retStr$
  197.         Local AMPM$
  198.         Local hrs
  199.         Local strMin$
  200.         Local strSec$
  201.        
  202.         ; meridiem
  203.         If thiswHour > 11
  204.                 AMPM$ = "PM"
  205.         Else
  206.                 AMPM$ = "AM"
  207.         EndIf
  208.        
  209.         ; convert from military time
  210.         hrs = thiswHour Mod 12
  211.         If hrs = 0 hrs = 12
  212.        
  213.         ; fix minute/second strings to 2 places
  214.         If thiswMinute < 10
  215.                 strMin = "0" + thiswMinute
  216.         Else
  217.                 strMin = Str(thiswMinute)
  218.         EndIf
  219.         If thiswSecond < 10
  220.                 strSec = "0" + thiswSecond
  221.         Else
  222.                 strSec = Str(thiswSecond)
  223.         EndIf
  224.        
  225.         ;concatenate
  226.         retStr = Str(thiswMonth) + "/" + Str(thiswDay) + "/" + Str(thiswYear) + " " + Str(hrs) + ":" + strMin + ":" + strSec + " " + AMPM$
  227.        
  228.         Return retStr
  229.        
  230. End Function
  231.  
  232. ;==========================================================================
  233. ; FUNCTION: GetFileAttributesStr()
  234. ; PARAMS:
  235. ;                       filename$ : valid filename/path string
  236. ; CALLS:
  237. ;                       FileExists()
  238. ;                       api_GetFileAttributes() : kernel32.dll function - returns Long value with file attribute bit flags
  239. ; RETURNS:
  240. ;       String with accumulated file attributes, i.e. "A" for Archive, "D" for folder, "HSA" for Hidden, System and Archive
  241. ;
  242. ; OTHER:        N = normal, R = read only, H = hidden S = system V = volume
  243. ;               D = directory, A = archive, L = alias, C = compressed
  244. ;       If you want just the integer value, just use the api function api_GetFileAttributes( filename$ ) by itself
  245.  
  246. Function GetFileAttributesStr$( filename$ )
  247.         If Not FileExists( filename$ )
  248.                 Return ""
  249.         EndIf
  250.        
  251.         Local fAttr
  252.         Local retStr$
  253.         Local a
  254.        
  255.         fAttr = api_GetFileAttributes( filename$ )
  256.        
  257.         ; mask to 8-bits
  258.         fAttr = fAttr And $FF
  259.        
  260.         ; attribute normal
  261.         If fAttr = 0 Then
  262.                 retStr$ = "N"
  263.         Else
  264.       ; loop thru byte to see which bits are set
  265.                 For a = 1 To 8
  266.                         If fAttr And 1 Shl (a - 1) Then
  267.                                 ; bit is set, add proper code from string
  268.                                 retStr = retStr + Mid$(ATTR_STRING, a, 1)
  269.                         End If
  270.                 Next
  271.         End If
  272.  
  273.         Return retStr
  274. End Function
  275.  
  276. ;==========================================================================
  277. ; FUNCTION: FileExists()
  278. ; PARAMS:
  279. ;                       filename$ : valid filename/path string
  280. ; CALLS:
  281. ; RETURNS:
  282. ;       True if file exists, False if not
  283. ; OTHER:               
  284. ;
  285.  
  286. Function FileExists( filename$ )
  287.         Local retValue
  288.         If FileType( filename$ ) = 1
  289.                 retValue = True
  290.         Else
  291.                 retValue = False
  292.         EndIf
  293.         Return retValue
  294. End Function


Comments :


Andres(Posted 1+ years ago)

 Very good. I definately need it at some point.


markcw(Posted 1+ years ago)

 Example code to test out the functions with.
Code: [Select]
;Main

Graphics 640,480,0,2
SetBuffer BackBuffer()

filename$=RequestFile("Any file..","",False)

pDate.SYSTEMTIME=FileDateTime(filename$)
USdate$=GetFileDateStringFromType(pDate)
Attr$=GetFileAttributesStr(filename$)

Date$="Date="+pDatewDay+"/"+pDatewMonth+"/"+pDatewYear
Date$=Date$+", Time="+pDatewHour+":"+pDatewMinute+":"+pDatewSecond

Text 0,0,"filename="+filename$
Text 0,12,"USdate="+USdate$
Text 0,24,Date$+" DayOfWeek="+pDatewDayOfWeek
Text 0,36,"Attr="+Attr$

While Not KeyHit(1)
Wend
And kernel32.decls to just copy+paste.
Code: [Select]
.lib "kernel32.dll"

Api_GetFileTime%(hFile%,lpCreationTime*,lpLastAccessTime*,lpLastWriteTime*):"GetFileTime"
Api_CreateFile%(FileName$, dwDesiredAccess%, dwShareMode%, lpSecurity%, dwCreationDsp%, dwFlagsAndAttr%, hTemplate%):"CreateFileA"
Api_FileTimeToSystemTime%(lpFileTime*, lpSystemTime*):"FileTimeToSystemTime"
Api_CloseHandle%( hObject% ):"CloseHandle"
Api_FileTimeToLocalFileTime%(lpFileTime*, lpLocalFileTime*):"FileTimeToLocalFileTime"

;also, to get file attributes, add
Api_GetFileAttributes%( lpFilename$ ):"GetFileAttributesA"



Snarkbait(Posted 1+ years ago)

 Thx, markcw... you don't need the
Code: [Select]
pDate.SYSTEMTIME=New SYSTEMTIMEas the FileDateTime function instantiates it for you, call it like
Code: [Select]
pDate.SYSTEMTIME = FileDateTime( filename$ )



markcw(Posted 1+ years ago)

 Oops, thanks. Code updated. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal