# Setting file dates

**URL:** <https://forum.xojo.com/t/setting-file-dates/46091>\
**Category:** General\
**Created:** [November 2, 2018, 7:26pm UTC](https://forum.xojo.com/t/setting-file-dates/46091 "2018-11-02T19:26:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Martin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@Roger\_Martin](https://forum.xojo.com/u/Roger_Martin)\
**Post date:** [November 2, 2018, 7:26pm UTC](https://forum.xojo.com/t/setting-file-dates/46091/1 "2018-11-02T19:26:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [November 2, 2018, 9:17pm UTC](https://forum.xojo.com/t/setting-file-dates/46091/2 "2018-11-02T21:17:42Z")

</div>

From [this Microsoft document](https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-setfiletime):

> [@](#):
>
> To prevent file operations using the given handle from modifying the last access time, call SetFileTime immediately after opening the file handle and pass a FILETIME structure that has both the dwLowDateTime and dwHighDateTime members set to 0xFFFFFFFF

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.

---

<div class="post-metadata">

**Author:** ![Roger\_Martin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@Roger\_Martin](https://forum.xojo.com/u/Roger_Martin)\
**Post date:** [November 2, 2018, 9:29pm UTC](https://forum.xojo.com/t/setting-file-dates/46091/3 "2018-11-02T21:29:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Lambert](https://forum.xojo.com/user_avatar/forum.xojo.com/andrew_lambert/32/215_2.png) [@Andrew\_Lambert](https://forum.xojo.com/u/Andrew_Lambert)\
**Post date:** [November 2, 2018, 9:53pm UTC](https://forum.xojo.com/t/setting-file-dates/46091/4 "2018-11-02T21:53:33Z")

</div>

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:

```auto
  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
```

---

<div class="post-metadata">

**Author:** ![Roger\_Martin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@Roger\_Martin](https://forum.xojo.com/u/Roger_Martin)\
**Post date:** [November 3, 2018, 2:42pm UTC](https://forum.xojo.com/t/setting-file-dates/46091/5 "2018-11-03T14:42:05Z")

</div>

Andrew,

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

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:26pm UTC](https://forum.xojo.com/t/setting-file-dates/46091/6 "2020-10-29T19:26:44Z")

</div>


