检索文件创建或修改日期

问题描述 投票:13回答:6

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

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];

谁能看到我哪里出错了?

ios objective-c xcode last-modified nsdocumentdirectory
6个回答
19
投票

试试这个。我有同样的问题,并解决了下一个问题:

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
}

希望它有所帮助;)


13
投票

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
    }
}

6
投票

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

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
    }
}

3
投票

如果您收到错误:

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

你可以尝试通过在转换为NSURL之前将“file:///”附加到NSString文件路径来解决这个问题,它在我的案例中起作用。


2
投票

也可以这样做:

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

1
投票

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

方式1:

  • 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 * modificationDate =(__ bridge NSDate *)modifDate;
  • NSLog(@“修改日期%@”,modificationDate);

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

© www.soinside.com 2019 - 2024. All rights reserved.