Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the creation date of a file in C under Mac OS X?

Tags:

c

macos

Mac OS X stores file creation time, and I know how to read it with stat() from <sys/stat.h>.

I could not find a way, how to set the creation time in C. It must be possible somehow, as the utility SetFile can do it (SetFile is part of the command line tools package from Apple):

SetFile -d '12/31/1999 23:59:59' file.txt

How can I do it in C?

like image 445
mr_georg Avatar asked Nov 07 '15 19:11

mr_georg


People also ask

How do I change the creation date of a file Mac?

To change the Modified timestamp to the current date and time, type "touch -m" in Terminal, followed by one space. Then drag the file from Finder into Terminal and press "Enter." Enter a space after the time and drag the file into the Terminal window. Press "Enter" to make the change.

How do I find the date a file was created on a Mac?

But yes, just go to the Finder, press Command + F to bring up a Finder window, then change the filters to search "This Mac", and "Last modified date" is "after" <your date> to see all your modified documents.

How do I change file info on Mac?

Click the blue, smiling face icon in your Mac's Dock to open Finder. Click the file you want to change. Clicking the file one time will select that file without opening it. To edit the properties of more than one file at a time, hold down the Control key as you click each file.


1 Answers

You can use utimes.

If times is non-NULL, it is assumed to point to an array of two timeval structures. The access time is set to the value of the first element, and the modification time is set to the value of the second element.

And:

For file systems that support file birth (creation) times (such as UFS2), the birth time will be set to the value of the second element if the second element is older than the currently set birth time. To set both a birth time and a modification time, two calls are required; the first to set the birth time and the second to set the (presumably newer) modification time

As an example:

struct timeval times[2];
memset(times, 0, sizeof(times));

times[0].seconds = 946684799;  /* 31 Dec 1999 23:59:59 */
times[1].seconds = 946684799;

utimes("/path/to/file", &times);

If the modification time passed is older than the current creation time of the file, the creation time will be set. You can then call utimes again if you want to set a different modification time.

like image 81
James McLaughlin Avatar answered Oct 11 '22 20:10

James McLaughlin