开发者

Delete a folder and its subfolders in Objective-C / C

开发者 https://www.devze.com 2023-04-02 10:34 出处:网络
How can I dele开发者_StackOverflow社区te a folder and its subfolders in Objective-C / C on iOS?You can use NSFileManager :

How can I dele开发者_StackOverflow社区te a folder and its subfolders in Objective-C / C on iOS?


You can use NSFileManager :

BOOL success = [[NSFileManager defaultManager] removeItemAtPath:pathToFolder error:nil];


This question is similar to this. You can check the link or follow the answer I've provided: You can get document directory by using this:

NSString *directoryPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/"];

** Remove full directory path by using this:

BOOL success = [fileManager removeItemAtPath:directoryPath error:nil];
if (!success) {
    NSLog(@"Directory delete failed");
}

** Remove the contents of that directory using this:

NSFileManager *fileManager = [NSFileManager defaultManager];    
if ([fileManager fileExistsAtPath:directoryPath]) {
            NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:directoryPath];
            NSString *documentsName;
            while (documentsName = [dirEnum nextObject]) {
                NSString *filePath = [directoryPath stringByAppendingString:documentsName];
                BOOL isFileDeleted = [fileManager removeItemAtPath:filePath error:nil];
                if(isFileDeleted == NO) {
                    NSLog(@"All Contents not removed");
                    break;
                }
            }
            NSLog(@"All Contents Removed");
        }

** You can edit directoryPath as per your requirement.


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths[0] stringByAppendingPathComponent:@"Videos"];

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    if (!success) {
        NSLog(@"Directory delete failed");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消