Ooops
January 26, 2021, 06:36:16 AM

Author Topic: [bb] FileDate$() and GetFileTime$() by turtle1776 [ 1+ years ago ]  (Read 580 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : FileDate$() and GetFileTime$()
Author : turtle1776
Posted : 1+ years ago

Description : This program contains 2 main functions, FileDate$() and FileTime$(), which get the time and date that the given file was created, last accessed, or last written. These functions use Windows API calls.

Code :
Code: BlitzBasic
  1. ;===========
  2. ;GetFileTime
  3. ;===========
  4. ;By Patrick Lester (turtle1776)
  5.  
  6. ;This program contains 2 main functions, FileDate$() and FileTime$(), which
  7. ;get the time and date that the given file was created, last accessed, or last written.
  8. ;It also contains a number of lower-level functions that can get specific time
  9. ;and date information (month, year, day of week, etc.).
  10.  
  11. ;These functions use Windows API calls, so you need to first add the following
  12. ;to your .decls text file (mine is called user32.decls) in your Blitz userlibs folder.
  13.  
  14. ;.lib "kernel32.dll"
  15. ;api_OpenFile% (lpFileName$, lpReOpenBuff*, wStyle%) : "OpenFile"
  16. ;api_GetFileTime% (hFile%, lpCreationTime*, lpLastAccessTime*, lpLastWriteTime*) : "GetFileTime"
  17. ;api_FileTimeToLocalFileTime% (lpFileTime*, lpLocalFileTime*) : "FileTimeToLocalFileTime"
  18. ;api_FileTimeToSystemTime% (lpFileTime*, lpSystemTime*) : "FileTimeToSystemTime"
  19. ;api_CloseHandle% (hObject%) : "CloseHandle"
  20.  
  21. ;For more information, see:
  22. ;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/time_reference.asp
  23. ;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/openfile.asp
  24. ;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/closehandle.asp
  25.  
  26. ;=====================================================
  27.  
  28.  
  29. Print FileDate$("C:Program FilesBlitzPlusBlitzPlus.exe",2)
  30. Print FileTime$("C:Program FilesBlitzPlusBlitzPlus.exe",2)
  31. WaitKey()
  32. End
  33.  
  34.  
  35.  
  36. ;====================================
  37. ;MAIN FUNCTIONS
  38.  
  39. ;This function uses the functions below to create a string containing
  40. ;the file date in the following format: MM-DD-YYYY.
  41. ;- Mode 0 = original file creation time
  42. ;- Mode 1 = last access time
  43. ;- Mode 2 = last write time
  44. Function FileDate$(file$,mode=2)
  45.         bank=GetFileTime(file$,mode)   
  46.         month = FileMonth(bank) : myMonth$ = month
  47.         If month < 10 Then myMonth$ = "0"+ month               
  48.         day = FileDay(bank) : myDay$ = day
  49.         If day < 10 Then myDay$ = "0"+ day
  50.         year = FileYear(bank)
  51.         FreeBank bank
  52.         Return myMonth$ + "-" + myDay$ + "-" + year
  53. End Function
  54.  
  55. ;This function uses the functions below to create a string containing
  56. ;the file time in hours, minutes and seconds as follows: HH:MM:SS
  57. ;Hours are returned in 0-23. You will need to convert to AM or PM if
  58. ;that is needed.
  59. ;- Mode 0 = original file creation time
  60. ;- Mode 1 = last access time
  61. ;- Mode 2 = last write time
  62. Function FileTime$(file$,mode=2)
  63.         bank=GetFileTime(file$,mode)           
  64.         hour = FileHour(bank) : myHour$ = hour
  65.         If hour < 10 Then myHour$ = "0"+ hour
  66.         minute = FileMinute(bank) : myMinute$ = minute
  67.         If minute < 10 Then myMinute$ = "0"+ minute
  68.         second = FileSecond(bank) : mySecond$ = second
  69.         If second < 10 Then mySecond$ = "0"+ second
  70.         FreeBank bank  
  71.         Return myHour$ + ":" + myMinute$ + ":" + mySecond        
  72. End Function
  73.  
  74.  
  75. ;====================================
  76. ;LOWER LEVEL UTILITY FUNCTIONS
  77.  
  78. ;This function gets the file times for a given file and stores them in a
  79. ;bank, which is returned from the function. The time and date info
  80. ;can then be read from the bank using the other functions below. Don't
  81. ;forget to free the bank after you are done with it, as is done in the
  82. ;main 2 functions above.
  83. ;- Mode 0 = original file creation time
  84. ;- Mode 1 = last access time
  85. ;- Mode 2 = last write time
  86. Function GetFileTime(file$,mode=2)
  87.         If mode < 0 Or mode > 2 Then Return ;error
  88.         If file$ = "" Then Return ; error
  89.         lpReOpenBuff = CreateBank (150)
  90.         lpCreationTime = CreateBank (8)
  91.         lpLastAccessTime = CreateBank (8)
  92.         lpLastWriteTime = CreateBank (8)
  93.         lpLocalFileTime = CreateBank (8)
  94.         lpSystemTime = CreateBank (16) 
  95.        
  96.         hFile = api_OpenFile% (file$, lpReOpenBuff, 0)
  97.         If hFile <> -1 ;if hFile = -1 then error, can't open file
  98.                 result = api_GetFileTime (hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime)
  99.                 If result = 1 ;if result <> 1 then error, can't get file time                  
  100.                         If mode = 0 Then result = api_FileTimeToLocalFileTime(lpCreationTime,lpLocalFileTime)
  101.                         If mode = 1 Then result = api_FileTimeToLocalFileTime(lpLastAccessTime,lpLocalFileTime)
  102.                         If mode = 2 Then result = api_FileTimeToLocalFileTime(lpLastWriteTime,lpLocalFileTime)         
  103.                         If result = 1 ;if result <> 1 then error, can't convert to local time
  104.                                 result = api_FileTimeToSystemTime (lpLocalFileTime,lpSystemTime)
  105.                                 ;note: if result <> 1 then function failed
  106.                         End If
  107.                 End If
  108.                 api_CloseHandle(hFile)                         
  109.         End If 
  110.  
  111.         ;Clean up and end function     
  112.         FreeBank lpReOpenBuff : FreeBank lpCreationTime : FreeBank lpLastAccessTime
  113.         FreeBank lpLastWriteTime : FreeBank lpLocalFileTime
  114.         Return lpSystemTime
  115. End Function
  116.  
  117. Function FileYear(bank)
  118.         If bank <= 0 Then Return ;invalid bank address
  119.         Return PeekShort(bank,0)
  120. End Function
  121.  
  122. Function FileMonth(bank)
  123.         If bank <= 0 Then Return ;invalid bank address
  124.         Return PeekShort(bank,2)
  125. End Function
  126.  
  127. Function FileDayOfWeek(bank)
  128.         If bank <= 0 Then Return ;invalid bank address
  129.         Return PeekShort(bank,4);0 = Sunday, 1= Monday, etc. ... 6 = Saturday
  130. End Function
  131.  
  132. Function FileDay(bank)
  133.         If bank <= 0 Then Return ;invalid bank address
  134.         Return PeekShort(bank,6);0-31
  135. End Function
  136.  
  137. Function FileHour(bank)
  138.         If bank <= 0 Then Return ;invalid bank address
  139.         Return PeekShort(bank,8) ;0-23 (you will need to convert to AM/PM as needed)
  140. End Function
  141.  
  142. Function FileMinute(bank)
  143.         If bank <= 0 Then Return ;invalid bank address
  144.         Return PeekShort(bank,10) ;0-59
  145. End Function
  146.  
  147. Function FileSecond(bank)
  148.         If bank <= 0 Then Return ;invalid bank address
  149.         Return PeekShort(bank,12) ;0-59
  150. End Function
  151.  
  152. Function FileMillisec(bank)    
  153.         If bank <= 0 Then Return ;invalid bank address
  154.         Return PeekShort(bank,14) ;0-999
  155. End Function


Comments :


Nikko(Posted 1+ years ago)

 great code!


Graythe(Posted 1+ years ago)

 Thanks


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal