Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I delete a folder and its subfolders in Objective-C / C on iOS?

like image 948
Maxime Avatar asked Sep 02 '11 03:09

Maxime


People also ask

How do I delete a folder and subfolders?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do I delete all subfolders and keep files?

In the search box (top right), type NOT kind:folder . You will now see a list of all files in your current folder and all its subfolders. Use Control-A to select all the files. Now you can move them all to another folder.

How do I delete all subfolders in CMD?

Run the command RMDIR /Q/S foldername to delete the folder and all of its subfolders.


2 Answers

You can use NSFileManager :

BOOL success = [[NSFileManager defaultManager] removeItemAtPath:pathToFolder error:nil]; 
like image 149
gcamp Avatar answered Sep 30 '22 15:09

gcamp


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.

like image 38
Asif Newaz Avatar answered Sep 30 '22 17:09

Asif Newaz