Breaking News
Home / iOS / iOS Snippets / How to remove all files of a particular type from a directory in iOS?

How to remove all files of a particular type from a directory in iOS?

Lets say you want to remove(delete) all the files of a particular extension say PNG from the Documents folder of the app. We can use the following code snippet.


NSString *fileExt = @"png";
// get the file manager
NSFileManager *fmanager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// get the doc directory
NSString *documentsDirectory = [paths objectAtIndex:0];
// get contents
NSArray *contents = [fmanager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
// enum
NSEnumerator *e = [contents objectEnumerator];
NSString *fname;
// loop
while ((fname = [e nextObject])) {
// compare
    if ([[fname pathExtension] isEqualToString:fileExt]) {
     // remove it
        [fmanager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:fname] error:NULL];
    }
}
 

[pb_builder]

Check Also

Your IAPs are set to Consumable – App Rejection Reason

App Rejection Reason We found that the Purchasability Type for one or more of your …

Advertisment ad adsense adlogger