Monday, January 2, 2012

Remove the directory; remove the specific files in the directory


1. Create the directory and remove the directory:

- (NSMutableString*)getUserDocumentDir {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSMutableString *path = [NSMutableString stringWithString:[paths objectAtIndex:0]];
    return path;
}

- (BOOL) createMyDocsDirectory{
    NSMutableString *path = [self getUserDocumentDir];
    [path appendString:@"/MyDocs"];
    NSLog(@"createpath:%@",path);
    return [[NSFileManager defaultManager] createDirectoryAtPath:path                            withIntermediateDirectories:NO attributes:nil error:NULL];
}

- (BOOL) deleteMyDocsDirectory {
    NSMutableString *path = [self getUserDocumentDir];
    [path appendString:@"/MyDocs"];
    return [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
Source: http://stackoverflow.com/questions/3664694/how-to-remove-a-directory-and-its-contents-using-nsfilemanager

2. Remove the jpg files in the directory:
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];
    }
}
Source: http://stackoverflow.com/questions/5819117/remove-file-types-from-documents-directory

No comments:

Post a Comment