Hi

is it possible to get the file creation date of files on the mac using c++? If so, may you assist me on how do I go about doing so?

Many thanks

Recommended Answers

All 4 Replies

do {		
		FSRef ref;
		if (noErr != FSPathMakeRef((const UInt8 *)filePath, &ref, NULL))
			break;
		FSCatalogInfo catalogInfo;
		if (noErr != FSGetCatalogInfo(&ref, kFSCatInfoCreateDate, &catalogInfo, NULL, NULL, NULL))
			break;
		UTCDateTime createDate = catalogInfo.createDate;
		CFAbsoluteTime absTime;
		if (noErr != UCConvertUTCDateTimeToCFAbsoluteTime (&createDate, &absTime))
			break;
		CFGregorianDate gregorianDate = CFAbsoluteTimeGetGregorianDate (absTime, NULL);
} while (false);

CFGregorianDate is a struct containing fields such as year, month, day etc.

Many thanks for your contribution. Please may you explain it. I would rather understand than simply copy and paste. Please if you do not mind.

Many thanks.

basically:
FSPathMakeRef creates a FSRef object from a given file path.
This object is passed to FSGetCatalogInfo function which returns an info about the file. kFSCatInfoCreateDate is a flag that notifies you're interested only in the creation date. You can combine different flags to obtain information about the file, which returns in catalogInfo object. Since you're interested only in the creation time, you can then access it via catalogInfo.createDate field. It's saved as UTCTime object. If you want to convert it to readable format, you first need to convert it to absolute time (using UCConvertUTCDateTimeToCFAbsoluteTime), and then to CFGregorianDate by using CFAbsoluteTimeGetGregorianDate.

BTW, developer.apple.com is an excellent source of information, you can read about all these methods there.

commented: Useful. Thanks +2

Magic, thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.