Setting File Time in C/C++

It was asked recently how one might go about changing the various access times (last modified, creation date etc) of an existing file through C/C++. The idea we will show you below takes advantage of a few functions in windows.h for altering file and system times. And no it can’t be used on your homework to change the due date. And no it doesn’t include the creation of a flux capacitor. But it will involve getting down with bad ass library windows.h. So lets kick off another entry of the Programmer’s Underground!

As I had mentioned before it was brought up as a question on the board about changing the last modified time of a file through C/C++ and I thought it was worth going over again on my blog. Here it shall reside as a permanent fixture, in stone, through the centuries. Hopefully it will help thousands of programmers everywhere.

A quick note about windows.h, this library may not be included by default in your version of visual studio. However express editions for 2008 should have it. If you find that the code here is simply not working and the functions appear undefined, be sure to download the windows sdk platform which should add in windows.h. You may have to do this if you are working with Visual studio 2005 express.

Now with that out of the way, lets take a quick gander at the code…

#include <windows.h>

int main()
{
   // Create a systemtime struct
   SYSTEMTIME thesystemtime;

   // Get current system time and then change the day to the 3rd
   // You can also change year, month, day of week etc
   GetSystemTime(&thesystemtime);
   thesystemtime.wDay = 3;

   // Create a FILETIME struct and convert our new SYSTEMTIME
   // over to the FILETIME struct for use in SetFileTime below
   FILETIME thefiletime;
   SystemTimeToFileTime(&thesystemtime,&thefiletime);

   // Get a handle to our file and with file_write_attributes access
   HANDLE filename = CreateFile("c:\\filenamehere.txt", FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
   
   // Set the file time on the file
   SetFileTime(filename,(LPFILETIME) NULL,(LPFILETIME) NULL,&thefiletime);
   
   // Close our handle.
   CloseHandle(filename);
   
   return 0;
}

The code above takes advantage of a few windows.h functions and structures. Two structures we will be dealing with are SYSTEMTIME and FILETIME. We create a new SYSTEMTIME struct which will use a function called GetSystemTime to get a copy of the current system time. We then access the various members of this structure to change parts of the time. In our example we modify the day to be the 3rd of the month. We have access to days, day of week, month, years, hour, minutes, seconds and even milliseconds.

After we alter this SYSTEMTIME struct, we use a function to convert this structure to a FILETIME structure. We do this because our upcoming function SetFileTime requires a FILETIME structure for its parameters. The SystemTimeToFileTime function takes two pointers. One to the SYSTEMTIME struct we just created and another to our FILETIME struct which it fills (it is an “out” parameter). Notice the use of ampersands here to give the function the memory addresses. So now we have a FILETIME structure representation of the SYSTEMTIME structure we modified at the beginning.

Next we need to get a handle to a file we want to change. We do this through the use of CreateFile and giving it a filename… plus specifying a few parameters. Among these parameters is the access mode which we need to say that the file attributes are writable. The other parameters talk about how the file is to be accessed during the time we have the file open (sharing modes) etc.

Once we have the handle, we have everything we need to alter its time. We use the SetFileTime and pass it the file handle we have plus three more parameters. Each of these three parameters alter various dates of the file. One is for creation time, one is for last accessed time, and one is last modified time. So we could set all three to the same FILETIME struct or set them individually. Here we are only setting the last modified/write time.

Last thing we do is close our handle with the CloseHandle() function. Make sure this part is done so that it frees up our handle to the file. Otherwise other processes may have trouble accessing the file.

Once the program is done, the file should be modified and all should be right with the world. You will go onto fame and fortune, rule an empire, and have your very own harem which you can share with the rest of the guys at Dream.In.Code. 😉

Like many other things in C++, this is only one way of doing it. There are other tricks you can do to alter the time but this one by far is pretty simple and straight forward. Removing my comments the file is only about 10 – 15 lines. If for some reason you don’t see the time of the file change at all, be sure you are specifying the full path to the file properly and that you actually have permissions to modify the file.

I hope you find it useful and enjoy it. Until next time, keep your nose to the grindstone and visit back often. You never know, I might put naked pictures of axel’s sister or give away free money (I doubt it though).

As always, thanks for reading! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 18 years. He works for a hot application development company in Vancouver Canada which service some of the biggest telecoms in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.