views:

43

answers:

1

hi all...

i want to know the contents in a directory either its documents or any other. if there is a file or more than one i need those file names. actually i am creating the export directory in documents directory.. and if export is empty then i am copying the zip file from main bundle to export folder

but the following code is not working. though the export is empty its not going in to that if block..r there any hidden files..? and in the last for loop its not doing any thing.

how to do this.

please help me out

    self.fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    self.documentsDir = [paths objectAtIndex:0];
    //creating folder 'export' to recieve the file from iTunes
    NSString *srcFilePath = [NSString stringWithFormat:@"%@/export", self.documentsDir];
    [fileManager createDirectoryAtPath:srcFilePath 
           withIntermediateDirectories:NO
                            attributes:nil
                                 error:nil];
    //copying the zip file into exprort from bundle if export is empty
    if(![fileManager fileExistsAtPath:srcFilePath]) {
        NSLog(@"File exists at path: %@", srcFilePath);
        NSString *resZipfile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"zip" 
                                                         inDirectory:@"pckg"];
        NSLog(@"zip file path ...%@", resZipfile);
        NSData *mainBundleFile = [NSData dataWithContentsOfFile:resZipfile];
        [[NSFileManager defaultManager] createFileAtPath:srcFilePath 
                                                contents:mainBundleFile 
                                              attributes:nil];      
    }
    NSString *eachPath;
    NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:srcFilePath];

    for(eachPath in dirEnum) NSLog(@"FILE: %@", eachPath);
+1  A: 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

NSFileManager *manager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *fileEnumerator = [manager enumeratorAtPath:documentsPath];

for (NSString *filename in fileEnumerator) {
    // Do something with file
}

[manager release];
Can Berk Güder
not sure that its working.i added the full code..
rockey
`fileExistsAtPath:` doesn't check if the `export` directory contains any files, it just checks if it exists. since you create it on the previous line, the condition is always false.
Can Berk Güder
ok..Thanks Can.
rockey