Search Amazon:

Set File Creation, Modified, Last Accessed Dates

Often times you may need to touch a file by setting its Created, Modified or Last Accessed date and time. This sample shows how to do it.

Set file creation, modified and last accessed dates.
Download Source Code

Read File Creation, Modified, Last Accessed Dates

The program starts by reading current file times from an existing file. It does this by opening the file with a call to CreateFile using GENERIC_READ access and a share mode of FILE_SHARE_READ Or FILE_SHARE_WRITE. Since the file exists, the creation disposition of OPEN_EXISTING is used.

The file handle returned by CreateFile is passed to the GetFileTime API which retrieves the created, accessed and modified times in corresponding FILETIME structures. A FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601. A call to CloseHandle closes the file.

The FileTimeToLocalFileTime API converts the time in our FILETIME structures based on the computer's local settings for time zone and Daylight Savings Time.

Lastly, the file times are converted into a more friendly format. A call to FileTimeToSystemTime converts the FILETIME structure to a SYSTEMTIME structure which represents a date and time using individual fields for month, day, year, hour,... These fields are then concatenated together and displayed as a normal date/time value.

Set File Creation, Modified, Last Accessed Dates

Setting filetimes is very similary to reading them with a few exceptions. A call to SetAttr is used to set the file's attributes to vbNormal. This removes the read-only flag if set. Also, GENERIC_WRITE access is specified in the CreateFile call.

The date to set is converted to a FILETIME value by parsing it into a SYSTEMTIME structure then using SystemTimeToFileTime to return the FILETIME. The local time conversion is performed and we are ready to invoke SetFileTime to set the dates.

SetFileTime takes as parameters the handle to the file and FILETIME structures for creation, last accessed and modified times. To leave a particular time value as is, leave the corresponding value null. After this command the file is closed and the original attributes are reapplied.

Instructions

Download the project and press F5 to run the program. Enter a valid path to a file and click Get File Values to retrieve the file's current date/time values.

Enter a new date/time values and click the Set buttons to set the value(s). Right click the file and select Properties in Windows Explorer to verify the new values.

Sign In
  User Id 
  Password 


Submit Your Own Code and Articles




About TheScarms
About TheScarms

Ask me your programming questions

I read every email and answer all I can.

User Feedback: Comments: 1 Items to Show:     

     
Id:  1 Posted:  6/6/2007 10:35:56 PM By:  EPRUBEN
Do you have code to do this in C#?
 
You must log in to post feedback.
Comment:    
 

If you use this code, please mention "www.TheScarms.com"

Email this page


TheScarms AppSentinel lets you securely copy protect and create evaluation versions of your software

TheScarms(tm) AppSentinel lets you quickly and easily create evaluation versions of your software and stop unauthorized copying and unregistered use of your programs!

Get your free
trial copy today!


      The World's Number 1 Web Host

© Copyright 2008 TheScarms