Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How to modify a files 'created' timestamp?

I need to modify the 'created' (if exists), 'modified' and 'accessed' timestamps of a file. Ideally this would be a platform-independent solution.

I've looked around the boost libraries but I can't see anything relevant. The nearest I've found to something relevant is this for Windows.

Can anyone help? Thanks.

like image 479
Ben L Avatar asked Sep 28 '10 15:09

Ben L


People also ask

Can you change the timestamp of a file?

Unfortunately, this isn't possible. You can view certain and change certain file attributes in File Explorer, but you can't change the last viewed, edited, or modified dates.

How do I edit the time created on a file?

You can modify the date created by copying a file. The file's created date becomes the modified date and the current date (when the file is copied) becomes the created date. You can copy a file on your PC to check.

Which command change timestamp of a file?

The default behavior of touch command is to change all three timestamps associated with a file to the current system time.


3 Answers

I've never used them but i guess that you are looking for the attribute functions:
http://www.boost.org/doc/libs/1_44_0/libs/filesystem/v2/doc/reference.html#Attribute-functions

There are also functions for the last modification:

template <class Path> std::time_t last_write_time(const Path& p);
template <class Path> void last_write_time(const Path& p, const std::time_t new_time);
like image 198
Daniel Avatar answered Oct 02 '22 03:10

Daniel


Another, slightly simpler code snippet for Windows.

like image 25
Ben L Avatar answered Oct 03 '22 03:10

Ben L


Use the utime function and utimbuf struct. The method is available in Windows but is named with a leading underscore as _utime.

Update: utime only allows you to change the access and modification times (via utimbuf's actime and modtime fields). This is most likely because many Unix-style file systems do not record the creation time anywhere.

like image 20
Richard Cook Avatar answered Oct 02 '22 03:10

Richard Cook