Have you ever needed to change a file’s Date Created, Date Modified, …
For the later it is simple! For the former, things are much more complicated.
Updating a File’s Date Modified
Changing the Date Modified is actually ‘pretty’ straightforward.
Now, one might think (and rightfully so), that you could use the FSO File DateLastModified property for this, but sadly it is read-only. That said, instead, we need to use the Folder Item’s ModifyDate. Once we realize that, it’s just a question of writing a wrapper procedure, such as:
VBScript Version
'Change the Date Modified for the specified file
' https://learn.microsoft.com/en-us/windows/win32/shell/folderitem-modifydate
Dim oFSO 'As Object
Dim oShell 'As Object
Dim oFile 'As Object
Dim oFolder 'As Object
Dim sFile 'As String
sFile = "C:\Temp\File Properties\test.txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Shell.Application")
Set oFile = oFSO.GetFile(sFile)
Set oFolder = oShell.NameSpace(oFile.ParentFolder.Path)
oFolder.Items.Item(oFile.Name).ModifyDate = DateSerial(2000, 1, 12) + TimeSerial(5, 35, 17)
Set oFolder = Nothing
Set oFile = Nothing
Set oShell = Nothing
Set oFSO = Nothing
You need only change the sFile value and then update the DateSerial/TimeSerial values for the date/time you wish to update the Date modified to.
VBA Version
Similarly, in VBA we can do something along the lines of:
'---------------------------------------------------------------------------------------
' Procedure : File_Set_DateModified
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Update the Date Modified of the specified file
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding -> None required
' References:
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFile : Fully qualified path and filename to update the Date Modified of
' dtDateTime: The new date/time to apply as the Date Modified, if omitted the current
' Date and Time will be used
'
' Output Variable:
' ~~~~~~~~~~~~~~~~
' True : File's Modify Date changed
' False : File's Modify Date NOT changed
'
' Usage:
' ~~~~~~
' Set the file's Date Modified to Now()
' ? File_Set_DateModified("C:\Temp\File Properties\test.txt")
' Returns -> True
'
' Set the file's Date Modified to 2000/01/12 5:35:17
' ? File_Set_DateModified("C:\Temp\File Properties\test.txt", _
' DateSerial(2000, 1, 12) + TimeSerial(5, 35, 17))
' Returns -> True
'
' Set the file's Date Modified to 12/31/2001 15:22:11
' ? File_Set_DateModified("C:\Temp\File Properties\test.txt", #12/31/2001 15:22:11#)
' Returns -> True
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2022-11-28 Initial Release
'---------------------------------------------------------------------------------------
Function File_Set_DateModified(sFile As String, Optional dtDateTime As Date) As Boolean
'Change the Last Modified / Date Modified of a File
' https://learn.microsoft.com/en-us/windows/win32/shell/folderitem-modifydate
' There is no FSO CreateDate property, nor LastAccessDate property!
On Error GoTo Error_Handler
Dim oFSO As Object
Dim oShell As Object
Dim oFile As Object
Dim oFolder As Object
Set oFSO = CreateObject("Scripting.Filesystemobject")
Set oShell = CreateObject("Shell.Application")
Set oFile = oFSO.GetFile(sFile)
Set oFolder = oShell.NameSpace(oFile.ParentFolder.Path)
If (TimeValue(Date) = dtDateTime) Then dtDateTime = Now
oFolder.Items.Item(oFile.Name).ModifyDate = dtDateTime
File_Set_DateModified = True
Error_Handler_Exit:
On Error Resume Next
Set oFolder = Nothing
Set oFile = Nothing
Set oShell = Nothing
Set oFSO = Nothing
Exit Function
Error_Handler:
' If Err.Number = 53 Then 'File not found
' 'Silent
' Else
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: File_Set_DateModified" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
' End If
Resume Error_Handler_Exit
End Function
and then we can use it by siomply doing:
Set the Date Modified to the Current Date/Time
Call File_Set_DateModified("C:\Temp\File Properties\test.txt")
Set the Date Modified to a Specific Date/Time
Call File_Set_DateModified("C:\Temp\File Properties\test.txt", _
DateSerial(2000, 1, 12) + TimeSerial(5, 35, 17))
Or
Call File_Set_DateModified("C:\Temp\File Properties\test.txt", #12/31/2001 15:22:11#)
Sadly, FSO does not expose either the Date Created, nor the Date Last Accessed. So for these we need to seek another solution.
Updating a File’s Date Created
There are ways to use Bat files, with elevated privileges, that change the system date/time, copy the file, rename it, and then set the date/time back, but I found them to be buggy when tested.
A better solution might be to employ a utility such as FileTouch.exe which was designed just for this task and emulates Unix’s/Linux’s touch command. That said, it isn’t always easy, in corporate environments, to get approval to use exe programs, especially those from 3rd parties.
Yet an even better approach is probably to simply to use Chip Peason’s VBA code, so take a look at:
He has sample downloads with all the required code.
I prefer this because it is plain VBA (yes, with lots of APIs!) and it is open-source so you can see exactly what is going on.
An example of it’s usage could be:
Call SetFileDateTime("C:\Temp\File Properties\test.txt", DateSerial(1988, 12, 22) + TimeSerial(8, 57, 11), FileDateCreate)
It can be used to update the Creation, Modification and/or Last Accessed dates by simply switching the last input variable.
Leider fehlt die Funktion “SetFileDateTime”
Ich möchte das “date of last modification” ändern.
In der Explorerdarstellung verschwindet dieses.
Was ist der Unterschied zwischen dem “date of last modification” und “ModifyDate”?