Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFileManager not deleting a file that exists

I am having some problem with using the NSFileManager functionalities. This happens both on the Simulator and an iPhone device (iOS 5.1).

Basically, I have a bunch of files that are stored in a document that I created. Now I am trying to move a file (stored at path) to the same directory with another name, to check if the deletion works.

if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
BOOL success = [[NSFileManager defaultManager] moveItemAtPath:path toPath:[path            stringByAppendingString:@".deleted"] error:&error];
if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
}

The output of this is both files at path and path.deleted. I ultimately just want to remove the file using removeItemAtPath but that is not working. It returns a success but if I see it in the file directory I can still see it there even after an hour.

like image 757
Devang Avatar asked May 16 '12 09:05

Devang


4 Answers

If you want to delete a file you should use removeItemAtPath:myPath error:NULL like

NSError *error;
if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
}
like image 152
cnu Avatar answered Nov 17 '22 09:11

cnu


I just figure it out of something very important when you use NSFileManager. You have to be aware of App Sandboxing.

let documentDirectory = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]

This line return the path document directory of your app sandboxed. When you create a file with FileManager in your document directory (for e.g) don't save the full file path but only the path from the current document directory.

You'll be able to recreate the full path of your created file.

Hope (after 5 years) help over developers ;-)

like image 40
Jean-Philippe Avatar answered Nov 17 '22 08:11

Jean-Philippe


Use this for search path and delete a video

(NSString *)applicationDocumentsDirectory {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
    }
NSString *dataFilePath = [[[self applicationDocumentsDirectory]
                               stringByAppendingPathComponent:@"Data.nosync"]
                              stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@.mp4",[dicForDelete valueForKey:keyVideoId],[dicForDelete valueForKey:keyVideoName]]];
NSError *error;
if ([[NSFileManager defaultManager] isDeletableFileAtPath:dataFilePath]) {
        BOOL success = [[NSFileManager defaultManager] removeItemAtPath:dataFilePath error:&error];
        if (success) {
          NSLog@"file deleted ");
        }else
        {
        NSLog@"file not deleted ");
        }
    }   
like image 31
Sanjay Kumar Yadav Avatar answered Nov 17 '22 08:11

Sanjay Kumar Yadav


I ran into the same issue. I'm still baffled as to why the fix works, but it's worth mentioning in case others can explain it.

I discovered that [[NSFileManager defaultManager] removeItemAtURL:url error:&error] would return false on the first attempt but with a nil error. On the second attempt it would return true.

Here's what my code looked like:

// success == NO error = nil
BOOL success = [[NSFileManager defaultManager] removeItemAtURL:storeUrl error:&error]; 

// success == YES error = nil
success = [[NSFileManager defaultManager] removeItemAtURL:storeUrl error:&error];

I discovered that if I created my own NSFileManager instance, it would work on the first try.

NSFileManager *fm = [[NSFileManager alloc] init];
[fm removeItemAtURL:storeUrl error:&error];

I'm running on the iOS9 simulator. This smells like a bug. I'm going to file a radar.

like image 44
David Avatar answered Nov 17 '22 09:11

David