Ravi shankar
Ravi shankar

Reputation: 2460

Deleteing more than one file from iphone documents folder?

My Iphone application is saving photos into document folder. I want to delete all these file form document folder after use. I know how to delete one file at time using path

if ([fileMgr removeItemAtPath:filePath error:&error] != YES)
    NSLog(@"Unable to delete file: %@", [error localizedDescription]);

but i want to delete all file in one go. All my files are in .png format I tried out *.png but not working.

Upvotes: 3

Views: 554

Answers (1)

sidyll
sidyll

Reputation: 59277

/* dir: the directory where your files are */
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *items = [fm contentsOfDirectoryAtPath:dir error:&error];
if (error) { /* ... */ }

/* delete png files */
for (NSString *item in items) {
    if ([[item pathExtension] isEqualToString:@"png"]) {
        NSString *path = [dir stringByAppendingPathComponent:item];
        [fm removeItemAtPath:path error:&error];
        if (error)
            { /* ... */ }
    }
}

Upvotes: 4

Related Questions