I want to delete the files which are placed in a folder(inside documents folder) of documents directory. I just listed or displayed all those files on the table view , can anybody tell me how can i do this using commitEditingStyle: method. Please help me how can i achieve this.Thanks in advance.
Asked
Active
Viewed 335 times
1 Answers
0
You can use the below code to delete the file from document directory:
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documents= [documentsDirectory stringByAppendingPathComponent:@"YourFolder"];
NSString *filePath = [documents stringByAppendingPathComponent:@"file2.txt"];
[fileMgr removeItemAtPath:filePath error:&error]

Midhun MP
- 103,496
- 31
- 153
- 200
-
Thanks for the quick reply re...but what i want is , i have first View Controller in that i am displaying all the folders of the documents directory on the table view , when i click on particular folder Second view controller will going to open in this i am displaying all the files inside that folder . so i don't want to give the hard coded folder name and file name. Instead of that i need to delete the selected folder or file from the table view and from documents directory . How can i do this please help me. – suvarna Jan 23 '13 at 07:28
-
@suvarna: In your delegate method of tableview you get the indexPath object. You need to fetch the corresponding file name from your array using indexPath.row. And pass that file name to the above code. It's a sample. You can modify it according to your need – Midhun MP Jan 23 '13 at 07:54
-
Yes re i tried and its getting deleted..Thank you very much for hint. – suvarna Jan 23 '13 at 12:22