在iphone中获取文档目录文件日期修改时间

时间:2012-12-13 07:26:53

标签: iphone ios

NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *currDircetory = [documentPath objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:currDircetory  error:nil];
for (NSString *s in filePathsArray){
        NSLog(@"file %@", s);
}

现在我使用上面的代码从文档目录中获取文件列表,现在我想知道如何在文档目录中修改文件的时间。

谢谢,

维贾雅恩

3 个答案:

答案 0 :(得分:11)

您可以使用:

NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *currDircetory = [documentPath objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:currDircetory  error:nil];

for (NSString *s in filePathsArray){

    NSString *filestring = [currDircetory stringByAppendingFormat:@"/%@",s];
    NSDictionary *filePathsArray1 = [[NSFileManager defaultManager] attributesOfItemAtPath:filestring error:nil];
    NSLog(@"Modified Day : %@", [filePathsArray1 objectForKey:NSFileModificationDate]);
}

答案 1 :(得分:7)

NSFileManager* filemngr = [NSFileManager defaultManager];

NSDictionary* attributes = [filemngr attributesOfItemAtPath:path error:nil];

if (attributes != nil) {

    NSDate *date = (NSDate*)[attributes objectForKey:NSFileModificationDate];
} else {

    NSLog(@"File Not found !!!");
}

答案 2 :(得分:0)

您可以得到答案Here

但只是改变你要做的是,你需要将NSFileCreationDate更改为NSFileModificationDate

NSDate *date = (NSDate*)[attrs objectForKey: NSFileModificationDate];