如何在Mac OS X中将文件的创建时间更改为较新的日期? [英] How do I change the creation time of a file to a newer date in Mac OS X?
问题描述
在Mac OS X上,使用C和/或C ++,我想将文件的创建时间更改为任意日期/时间。我找到了一些解决方案,特别是此StackOverflow答案,允许将创建时间设置为较旧日期,但这对我来说不够 - 我也希望能够设置较新的日期。因此,使用 utimes()
函数不是我正在寻找的解决方案。
On Mac OS X, using C and/or C++, I want to change the creation time of a file to an arbitrary date/time. I have found a number of solutions, notably this StackOverflow answer, that allow to set the creation time to an older date, but this is not sufficient for me - I also want to be able to set a newer date. Using the utimes()
function therefore is not the solution I am looking for.
我知道设置较新的创建时间必须以某种方式,因为从苹果的开发人员命令行工具的 SetFile
实用程序可以做到,但到目前为止我的搜索没有找到任何提示,带给我更接近解决方案。
I know that setting a newer creation time must be possible somehow, because the SetFile
utility from Apple's developer command line tools can do it, but so far my search-fu has failed to uncover any hints that bring me closer to a solution.
有没有人知道我如何实现我的目标?
Does anyone know of a way how I can achieve my goal?
为什么我想自己这样做,为什么我不能使用 SetFile
?
Why do I want to do this myself, why can't I use SetFile
?
-
SetFile
命令行实用程序已弃用(请参阅man SetFile
),因此它将在未来一段时间 - 我想创建一个实用程序,允许我指定一个时间增量来从当前创建时间添加/减去。
SetFile
实用程序没有任何方便的命令行参数来执行此操作。 - 最后但并非最不重要:
- The
SetFile
command line utility is deprecated (seeman SetFile
), so it's bound to go away some time in the future - I want to create a utility that allows me to specify a time delta to add to/subtract from the current creation time. The
SetFile
utility does not have any convenient command line arguments to do this. - Last but not least: Curiosity!
推荐答案
没有尝试,但根据文档, NSURL
下的键 NSURLCreationDateKey
下的资源值是读写。因为你指定了C或C ++,你将使用相应的 CFURL
API。所以,你可以调用:
Haven't tried it, but according to the docs, the NSURL
resource value under the key NSURLCreationDateKey
is read-write. Since you specified C or C++, you'd use the corresponding CFURL
API. So, you'd call:
CFURLRef url = /* ... */
CFDateRef date = /* ... */
CFErrorRef error;
if (!CFURLSetResourcePropertyForKey(url, kCFURLCreationDateKey, date, &error))
/* handle error */;
编辑:最小范例
A minimal example
const char* fileName = "/path/to/file";
size_t fileNameStringLength = strlen(fileName);
Boolean isDirectory = false;
CFURLRef url = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
(const UInt8*)fileName,
fileNameStringLength,
isDirectory);
// Seconds since 1 January, 2001 00:00:00 GMT
CFAbsoluteTime absTime = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime adjustedCreationTime = absTime - 3600;
CFDateRef date = CFDateCreate(
kCFAllocatorDefault,
adjustedCreationTime);
CFErrorRef error;
if (!CFURLSetResourcePropertyForKey(url, kCFURLCreationDateKey, date, &error))
{
fprintf(stderr, "an error occurred\n");
exit(1);
}
CFRelease(url);
CFRelease(date);
这篇关于如何在Mac OS X中将文件的创建时间更改为较新的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!