Search code examples
iphoneiosxcodejailbreak

How to get name and creation date of all files in a folder in ios?


I am using the following code to get creation date of a file at a given path and it works fine.

NSDictionary* attrs = [fm attributesOfItemAtPath:@"/Users/me/Desktop/ios" error:nil];

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

    }
    else {
        NSLog(@"Not found");
    }

Now I want to get the names and creation dates of all files in a folder given at a path.How can I do that? Can anybody help me?


Solution

  • first get list of all files from your directory & then loop until all file completes

    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
    for(int i=0;i<[dirContents count];i++)
    {
    NSDictionary* attrs = [fm attributesOfItemAtPath:[dirContents objectAtIndex:i] error:nil];
    
        if (attrs != nil) {
            NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
            NSLog(@"Date Created: %@", [date description]);
    
        }
        else {
            NSLog(@"Not found");
        }
    }