-3

I have this code, which allows me to specify a particular file to be deleted from my documents directory.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [[paths objectAtIndex:0] stringByAppendingString:@"/Podcasts"];

    NSString* checkIfFileExists = [documentsDirectoryPath stringByAppendingPathComponent:_fileName];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:checkIfFileExists error:NULL];

I can see this being useful if you want to delete a file with a button, but instead of wanting to only delete a particular file, how do I reference the removeItemAtPath: to handle any file within the array?? I do not want it to do delete all the files at once.

jwknz
  • 6,598
  • 16
  • 72
  • 115

3 Answers3

1

You can get the contents of a directory with - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error of NSFileManager.

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

Then loop over the array and delete the files one by one.

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
1

You can use the same code.

But need to add the file name to it like:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [[paths objectAtIndex:0] stringByAppendingString:@"/Podcasts"];
NSString *file = [documentsDirectoryPath stringByAppendingString:@"%@",[yourFileNamesArray objectAtIndex:0];

NSString* checkIfFileExists = [file stringByAppendingPathComponent:_fileName];

NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:checkIfFileExists error:NULL];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • yourFileNamesArray is that not the same as the NSArray *paths?? – jwknz Jan 04 '13 at 09:15
  • @JeffKranenburg: no, it's not same array , if you already have a array with the file names like: file1.txt, file2.txt etc – Midhun MP Jan 04 '13 at 09:18
  • I believe he thinks you want to remove multiple files, so have stored their names in an array. You can replace it with a string literal containing the proper file name and it'll work just fine. – CodaFi Jan 04 '13 at 09:19
  • ok, but if I have a string literal, unless I am mistaken, does that not hardcode the file name into the method above?? I want to give the user the ability to delete a single file that they have stored in the documents folder, but I do not know the that file – jwknz Jan 04 '13 at 09:22
  • The only array I have in this view is what is listed in the users Documents folder. – jwknz Jan 04 '13 at 09:24
  • Then you can use the `contentsOfDirectoryAtPath` method of NSFileManager, it''ll return an `NSArray`, you need to display it to user then according to user's selection, you can pass the file name to above code. – Midhun MP Jan 04 '13 at 09:26
1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [[paths objectAtIndex:0] stringByAppendingString:@"/Podcasts"];
NSString* checkIfFileExists = [documentsDirectoryPath stringByAppendingPathComponent:_fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isMyFileThere = [[NSFileManager defaultManager] fileExistsAtPath:checkIfFileExists];
if(isMyFileThere){

          [fileManager removeItemAtPath:checkIfFileExists error:NULL];
}
else{
          //file dont exists
}
Dhara
  • 4,093
  • 2
  • 36
  • 69