【问题标题】:Retrieve file creation or modification date检索文件创建或修改日期
【发布时间】:2012-11-09 22:54:18
【问题描述】:

我正在使用这段代码来尝试检索文件的最后修改日期:

NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath: myFilePath error:&error];

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

这适用于主包中的文件,但如果文件位于应用程序文档文件夹的子目录中,myFilePath 如下所示:

/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/The App ID Number/Documents/mySubdirectory/My Saved File

它一直返回“未找到”。

我知道文件在那里,因为我可以使用 finder 来查看它。我也尝试删除文件名中的空格,但这没有效果。

错误日志显示没有这样的文件或目录,所以当我试图将文件复制到文档目录时,它看起来一定出了问题。

奇怪的是,使用contentsOfDirectoryAtPath 遍历文档子目录会显示文件存在。

我已尝试对路径进行硬编码并以编程方式检索它:

*myFolder = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];
*myFilePath = [myFolder stringByAppendingPathComponent:theFileName];

谁能看出我哪里出错了?

【问题讨论】:

  • 文件名中的那三个点是什么?
  • 道歉;它们代表路径的其余部分,为简洁起见,我将其删除:/6.0/Applications/The App ID Number/。我已经编辑了问题并重新插入了它们。
  • 文件在你的xcode项目中吗?以编程方式创建?
  • @Tin Can:是的。然后我从文件夹中删除了它并手动添加了一个不同的文件,看看是否会有所作为。
  • @Robert Sherlock Holmes:“当你消除了不可能的事情时,剩下的无论多么不可能,都必须是事实。”您说您可以在 Finder 中看到该文件,因此请仔细检查您的文件路径。否则您的代码似乎没有问题。

标签: ios objective-c xcode last-modified nsdocumentdirectory


【解决方案1】:

Swift 3 解决方案:

func fileModificationDate(url: URL) -> Date? {
    do {
        let attr = try FileManager.default.attributesOfItem(atPath: url.path)
        return attr[FileAttributeKey.modificationDate] as? Date
    } catch {
        return nil
    }
}

【讨论】:

    【解决方案2】:

    试试这个。我遇到了同样的问题,并解决了类似下一个:

    NSURL *fileUrl = [NSURL fileURLWithPath:myFilePath];
    NSDate *fileDate;
    [fileUrl getResourceValue:&fileDate forKey:NSURLContentModificationDateKey error:&error];
    if (!error)
    {
    //here you should be able to read valid date from fileDate variable
    }
    

    希望它有所帮助;)

    【讨论】:

    • 感谢您的建议,尽管我不确定我是否理解正确。我试过这个:NSURL *fileURL = [NSURL URLWithString:fullPathURLString]; NSDate *fileDate; [fileURL getResourceValue:&fileDate forKey:NSURLContentModificationDateKey error:&error]; if (!error) { NSLog(@"file modification date = %@", fileDate); },但仍然为空。
    • 是的,文件存在,尽管文件名包含已被%20 替换的空格。这会导致问题吗?
    • 这是错误日志:CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme: /Users/User/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/A008CF0F-9EE1-42B5-921C-598FC8420279/Documents/downloads/Diagrams/8101-8101N%20Harsco%20Week%20.doc
    • 请不要接受答案,除非他们确实解决了您的问题,否则表明其他用户这确实代表了一个解决方案。情况可能并非如此。
    • 这是一个很好的答案,但是您应该使用来自 [NSURL getResourceValue::] 的返回值而不是 NSError 对象的存在来检查错误。
    【解决方案3】:

    这是@zvjerka24 答案的类似 Swift 的解决方案:

    func lastModified(path: String) -> NSDate? {
        let fileUrl = NSURL(fileURLWithPath: path)
        var modified: AnyObject?
        do {
            try fileUrl.getResourceValue(&modified, forKey: NSURLContentModificationDateKey)
            return modified as? NSDate
        } catch let error as NSError {
            print("\(#function) Error: \(error)")
            return nil
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果出现错误:

      “CFURLCopyResourcePropertyForKey 失败,因为它传递了这个没有方案的 URL”

      您可以尝试通过在将其转换为 NSURL 之前将“file:///”附加到您的 NSString 文件路径来解决此问题,它适用于我的情况。

      【讨论】:

      • 它有效,但您应该使用-fileURLWithPath: 而不是添加file:/// 前缀。
      【解决方案5】:

      还可以:

      NSURL* file = ...
      NSError* error;`
      NSDate *creationDate = [[NSFileManager defaultManager] attributesOfItemAtPath:file.path error:&error].fileCreationDate;
      

      【讨论】:

        【解决方案6】:

        对于 macOS 系统中的任何文件,我们可以使用以下任何选项轻松获取修改日期:

        方式一:

        • NSString *path = @"文件路径";
        • NSError *err = nil;
        • NSDictionary *dic2 = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&err];
        • NSLog(@"文件修改日期:%@", dic2[NSFileModificationDate]);

        方式 2:

        • MDItemRef itemRef = MDItemCreate(kCFAllocatorDefault, (__bridge CFStringRef)path);
        • NSArray *attributeNames = (__bridge NSArray *)MDItemCopyAttributeNames(itemRef);
        • NSDictionary *attributes = (__bridge NSDictionary *) MDItemCopyAttributes(itemRef, (__bridge CFArrayRef) attributeNames);

        • CFDateRef modifDate = MDItemCopyAttribute(itemRef, kMDItemContentModificationDate);

        • NSDate* modifyDate = (__bridge NSDate*) modifDate;
        • NSLog(@"修改日期%@", modifyDate);

        您还可以打印 MDItem 提供的各种其他属性: NSLog(@"所有属性%@", attributes);

        【讨论】:

          猜你喜欢
          • 2016-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-26
          • 1970-01-01
          • 1970-01-01
          • 2022-11-09
          相关资源
          最近更新 更多