40

我正在尝试查找文件的创建日期(不是修改日期)。

创建日期似乎不在文件的属性中,但修改日期在。

我正在使用此代码..

NSFileManager* fm = [NSFileManager defaultManager];

NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];

if (attrs != nil) {
    return (NSDate*)[attrs objectForKey: NSFileCreationDate];
} else {
    return nil;
}

这总是返回零。在调试器中键入“po attrs”以获取 NSDictionary 中的键/值对列表返回以下内容。

NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = 员工;
NSFileModificationDate = 2010-01-21 11:47:55 +0000;
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = 本;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 338;
NSFileSystemFileNumber = 2234;
NSFileSystemNumber = 42553324;
NSFileType = NSFileTypeRegular;

没有创建日期.. bah..

任何人都知道获取创建日期的另一种方法,还是它在 iOS 中不存在?

4

10 回答 10

72

此代码实际上向我返回了良好的创建日期:

NSFileManager* fm = [NSFileManager defaultManager];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];

if (attrs != nil) {
    NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
    NSLog(@"Date Created: %@", [date description]);
} 
else {
    NSLog(@"Not found");
}

您是在 App 内创建文件吗?也许这就是问题所在。

于 2011-06-21T16:28:24.510 回答
21

中有一个特殊的消息fileCreationDateNSDictionary以下对我有用:

目标-C:

NSDate *date = [attrs fileCreationDate];

迅速:

let attrs = try NSFileManager.defaultManager().attributesOfItemAtPath(path) as NSDictionary
attrs.fileCreationDate()
于 2011-11-03T09:26:16.470 回答
9

斯威夫特 2.0 版本:

do {
    let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(YOURPATH)
    let creationDate = fileAttributes[NSFileCreationDate] as? NSDate
    let modificationDate = fileAttributes[NSFileModificationDate] as? NSDate
    print("creation date of file is", creationDate)
    print("modification date of file is", modificationDate)
}catch let error as NSError {
    print("file not found:", error)
}
于 2015-11-13T15:05:06.720 回答
9

更新了Swift 4的答案以提取修改 ( .modifiedDate) 或创建 ( .creationDate) 日期:

let file: URL = ...
if let attributes = try? FileManager.default.attributesOfItem(atPath: file.path) as [FileAttributeKey: Any],
    let creationDate = attributes[FileAttributeKey.creationDate] as? Date {
    print(creationDate)
    }
  1. 使用您通过 URL 预先提供的文件,它将请求其属性。如果成功,则返回 [FileAttributeKey: Any] 字典
  2. 使用步骤 1 中的字典,然后提取创建日期(或根据需要修改)并使用条件展开,如果成功则将其分配给日期
  3. 假设前两个步骤成功,您现在有了一个可以使用的日期
于 2017-12-18T06:49:10.320 回答
7

在 Swift 4 中,如果您想知道 DocumentsDirectory 中特定文件的文件创建日期,可以使用此方法

func getfileCreatedDate(theFile: String) -> Date {

        var theCreationDate = Date()
        do{
            let aFileAttributes = try FileManager.default.attributesOfItem(atPath: theFile) as [FileAttributeKey:Any]
            theCreationDate = aFileAttributes[FileAttributeKey.creationDate] as! Date

        } catch let theError as Error{
            print("file not found \(theError)")
        }
        return theCreationDate
    }
于 2017-06-28T06:05:25.780 回答
6

在 Swift 5 中:

let date = (try? FileManager.default.attributesOfItem(atPath: path))?[.creationDate] as? Date
于 2019-10-19T02:48:38.440 回答
3
  NSDate *creationDate = nil;
  if ([fileManager fileExistsAtPath:filePath]) {
       NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
       creationDate = attributes[NSFileCreationDate];
  }

在这里

于 2013-11-20T08:48:23.707 回答
2

Swift 3 版本代码:

do {
        let fileAttributes = try FileManager.default.attributesOfItem(atPath: yourPathString)
        let modificationDate = fileAttributes[FileAttributeKey.modificationDate] as! Date
        print("Modification date: ", modificationDate)
    } catch let error {
        print("Error getting file modification attribute date: \(error.localizedDescription)")
    }
于 2017-02-14T19:06:25.453 回答
1

你可以fstat64用来获取st_birthtimespec返回结构的成员吗?您需要为该文件创建一个 C 文件句柄并将timespec值转换为NSDate,但这总比没有好。

于 2010-01-21T12:43:17.977 回答
1

斯威夫特 5

let url: URL = ....
let attributes = try? FileManager.default.attributesOfItem(atPath: url.path)
if let date = attributes?[.modificationDate] {
   print("File Modification date is %@", date)
}
if let date = attributes?[.creationDate] {
   print("File Creation date is %@", date)
}
于 2021-12-14T08:33:43.920 回答