12

i am trying to delete all the files in the Document Directory with the extension ".jpg", after many tries, i am still not managed to succeed.

the code i have already now is:

-(void)removeOneImage:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
}

with this code i can remove one file with a specific name.

Can anyone help me to remove all files with a specific extension?

Kind regards,

Snowy

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
Snowy
  • 139
  • 1
  • 2
  • 9

4 Answers4

37
    NSString *extension = @"jpg";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];  
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {

    if ([[filename pathExtension] isEqualToString:extension]) {

        [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
    }
}
Francis McGrew
  • 7,264
  • 1
  • 33
  • 30
3
// Get the Documents directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];

// Delete the file using NSFileManager
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[documentsDirectoryPath stringByAppendingPathComponent:yourFile.txt] error:nil];

For More information

How to delete ALL FILES in a specified directory on the app?

Community
  • 1
  • 1
Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51
2

very simple method:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *imageName = [NSString stringWithFormat:@"demo.jpg"];
[fileManager removeItemAtPath:imageName error:NULL];
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
1

If you prefer fast enumeration:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *extension = @"jpg";

NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

for (NSString *file in contents) {
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:file];

    if ( ([[file pathExtension] isEqualToString:extension]) && [fileManager isDeletableFileAtPath:filePath] ) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
}
Shebuka
  • 3,148
  • 1
  • 26
  • 43