Domanda

Vorrei sapere il contenuto di una directory sia i suoi documenti o qualsiasi altro. se v'è un file o più di quello che ho bisogno di quei nomi di file. in realtà sto creando la directory di esportazione nella cartella documenti .. e se l'esportazione è vuota allora sto copiando il file zip dal fascio principale per esportare la cartella

, ma il seguente codice non funziona. anche se l'esportazione è vuota non la sua intenzione a che se block..r c'è alcun file nascosti ..? e nell'ultimo ciclo for sua non fare qualsiasi cosa.

come fare questo.

ti prego di aiutarmi

    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);
È stato utile?

Soluzione

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

Altri suggerimenti

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docsPath = [paths objectAtIndex:0];
NSString *srcFilePath = [NSString stringWithFormat:@"%@/export", self.documentsDir];
BOOL isDir;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:srcFilePath] isDirectory:&isDir];
if (!exists && !isDir) {
    [fileManager createDirectoryAtPath:srcFilePath 
       withIntermediateDirectories:NO
                        attributes:nil
                             error:nil];
    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];      

Swift 4 Versione:

    guard let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first,
        let fileEnumerator = fileManager.enumerator(atPath: path) else {
        return
    }
    let fileNames = fileEnumerator.flatMap { $0 as? String } //Here You will have Array<String> with files in current folder and subfolders of your application's Document directory
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top