代码之家  ›  专栏  ›  技术社区  ›  Ben Clayton

iOS:如何找到文件的创建日期?

  •  30
  • Ben Clayton  · 技术社区  · 15 年前

    我试图找到一个文件的创建日期(不是修改日期)。

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

    我在用这个代码……

    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=员工;
    文件修改日期=2010-01-21 11:47:55+0000;
    nsFileOwnerAccountID=501;
    nsFileOwnerAccountName=Ben;
    nsFilePosiExpermissions=420;
    nsFileReferenceCount=1;
    文件大小=338;
    nsFileSystemFileNumber=2234;
    NSfileSystemNumber=42553324;
    nsFileType=nsFileTypeRegular;

    没有创建日期..啊……

    有人知道另一种获取创建日期的方法吗,或者它只是在iOS中不存在?

    8 回复  |  直到 7 年前
        1
  •  66
  •   Oriol Nieto    13 年前

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

    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");
    }
    

    你在应用程序中创建文件吗?也许这就是问题所在。

        2
  •  15
  •   ricardopereira    7 年前

    有个特别的消息 fileCreationDate 为了那个 NSDictionary . 以下对我有效:

    目标C:

    NSDate *date = [attrs fileCreationDate];
    

    Swift:

    let attrs = try NSFileManager.defaultManager().attributesOfItemAtPath(path) as NSDictionary
    attrs.fileCreationDate()
    
        3
  •  7
  •   Sam    9 年前

    Swift 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)
    }
    
        4
  •  4
  •   Naishta    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
        }
    
        5
  •  3
  •   Govind    11 年前
      NSDate *creationDate = nil;
      if ([fileManager fileExistsAtPath:filePath]) {
           NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
           creationDate = attributes[NSFileCreationDate];
      }
    

    它的 here

        6
  •  2
  •   mriaz0011    8 年前

    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)")
        }
    
        7
  •  1
  •   Jeff Kelley    15 年前

    你能用吗? fstat64 得到 st_birthtimespec 返回结构的成员?您需要为该文件创建一个C文件句柄并转换 timespec 价值转化为 NSDate 但这总比什么都没有好。

        8
  •  0
  •   CodeBender    7 年前

    更新了的答案 斯威夫特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. 假设前两个步骤成功,现在您有了一个可以与之合作的日期。