Search code examples
iphoneuitableviewdetailsviewnsfilemanager

iPhone - properties of a list of files


I have an mutableArray which displays a list of files in a tableView. I have stored those files in a directory.

I am using NSfileManager class and attributesOfItemAtPath method to retrieve some properties of the files like fileSize, fileType..

In the second stage I am trying to have a detailView controller which displays the properties of the particular file when touched in the tableView.

The problem is I dont know how to bind those properties like fileSize and fileType separately to a NSDictionary and make it display in the detailView for that particular file.

Here is my source code for listing the files and to list the properties.

- (void)listFiles {

    NSFileManager *fm =[NSFileManager defaultManager];
    NSError *error = nil;
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
    if (error) {
        NSLog(@"%@", [error localizedDescription]);
        error = nil;
    }
    directoryContent = [[NSMutableArray alloc] init];
    for (NSString *path in paths){
        NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
        [directoryContent addObject:documentsDirectory];
    }
}

-(void) willProcessPath {
    NSString *parentDirectory = @"/Users/akilan/Documents";
    NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
    for (NSString *path in paths){
        filesPath = [parentDirectory stringByAppendingPathComponent:path];
        NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
        filesSize = [attrDir objectForKey:NSFileSize];
        filesName = (NSString *)directoryContent;
        filesType = [path pathExtension];
        createdDate = [attrDir objectForKey:NSFileCreationDate];
        modifiedDate = [attrDir objectForKey:NSFileModificationDate];
    }
}

Please help me to proceed to the next step.. Thanks in advance..


Solution

  • Try something like this:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self listFiles];
    }
    
    
    
    - (void)listFiles {
    
        NSFileManager *fm =[NSFileManager defaultManager];
        NSError *error = nil;
        NSString *parentDirectory = [self getDocumentsPath];
        NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
    
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
            error = nil;
        }
    
        NSMutableArray *array = [[NSMutableArray alloc] init];
        self.directoryContent = array;
        [array release];
    
        for (NSString *path in paths){
            NSString *fullPath = [self getPathToFileInDocumentsDirectory:path];
            NSMutableDictionary *filePropertiesDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil]];
            [filePropertiesDictionary setObject:fullPath forKey:@"fullPath"];
            [filePropertiesDictionary setObject:path forKey:@"fileName"];
            [directoryContent addObject:filePropertiesDictionary];
        }
        NSLog(@"%@", directoryContent);
    }
    
    - (NSString *)getDocumentsPath {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    
        return documentsDirectoryPath;  
    }
    - (NSString *)getPathToFileInDocumentsDirectory:(NSString *)fileName {
        return [[self getDocumentsPath] stringByAppendingPathComponent:fileName];
    }