Setting file dates

Hi All,

I am working on a Windows based program that will allow files to be copied from one location to another, but have the copied file maintain the original file’s Created Date, Modified Date and Last Accessed Date. I am able to keep the Created and Modified Dates the same, but the Last Accessed Date always takes on the current date. Any ideas on how I would go about setting the Last Accessed Date to be the same as the original file?

Thanks in advance.

From this Microsoft document:

I’m sorry that I don’t have the experience to know how to make it work. Hope this helps and you can figure how.

Alberto,

This information is noted. However, I would truly prefer to do this from within Xojo itself and not have to make any calls using CMD line or Powershell.

Thanks.

I’m not sure Xojo exposes a way to affect the last access time. @Alberto De Poo 's suggestion is a good one, and it would go something like this:

  Declare Function GetFileTime Lib "Kernel32" (hFile As Integer, CreateTime As Ptr, AccessTime As Ptr, WriteTime As Ptr) As Boolean
  Declare Function SetFileTime Lib "Kernel32" (hFile As Integer, CreateTime As Ptr, AccessTime As Ptr, WriteTime As Ptr) As Boolean
  
  Dim SourceFolderItem As FolderItem = GetOpenFolderItem("")
  Dim DestinFolderItem As FolderItem = GetSaveFolderItem("", SourceFolderItem.Name)
  
  ' get a handle to the original file. BinaryStream.Handle will work 
  Dim bs As BinaryStream = BinaryStream.Open(SourceFolderItem)
  Dim hFile As Integer = bs.Handle(BinaryStream.HandleTypeWin32Handle)
  
  Dim create As New MemoryBlock(8) ' FILETIME struct
  Dim access As New MemoryBlock(8) ' FILETIME struct 
  Dim write As New MemoryBlock(8)  ' FILETIME struct
  
  ' populate structs with original dates
  Call GetFileTime(hFile, create, access, write)
  
  ' close the handle and copy the item
  bs.Close
  SourceFolderItem.CopyFileTo(DestinFolderItem)
  
  ' get a handle to the destination
  bs = BinaryStream.Open(DestinFolderItem, True)
  hFile = bs.Handle(BinaryStream.HandleTypeWin32Handle)
  
  ' use previously-filled structs on destination
  Call SetFileTime(hFile, create, access, write)
  bs.Close ' close the handle

Andrew,

Let me play around with this and see what results I get.

Thanks.