cyclingIsBetter
cyclingIsBetter

Reputation: 17591

IOS: delete a file in a directory

In my app I download a pdf file with an ASiHttpRequest and I have these instructions:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    [currentDownload setDownloadDestinationPath:[documentsDirectory stringByAppendingPathComponent:@"file.pdf"]];

it work fine at first time, and I can open this file.pdf, but when I download a second time this pdf, it seems that it not replace the file but do a merge.

before I do this, but it doesn't work where is the problem, or what's the best way to delete this file.pdf from its path?

    - (void) removeFile{
  NSString *extension = @"pdf";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPDF = [paths objectAtIndex:0];

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectoryPDF error:NULL];
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;
    while ((filename = [e nextObject])) {

        if ([[filename pathExtension] isEqualToString:extension]) {

            [fileManager removeItemAtPath:[documentsDirectoryPDF stringByAppendingPathComponent:filename] error:NULL];
        }
    }
}

EDIT

now I use this method

- (void) removeFile{


    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0]stringByAppendingString:@"/file.pdf"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);


        if([fileManager fileExistsAtPath:path] == YES)
        {
            NSLog(@"file exist and I delete it");

            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:path error:&error];

            NSLog(@"error:%@", error);
        }

    NSLog(@"Documents directory after: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);


}

this method recognize that in directory there is "file.pdf" in NSLog

NSLog(@"Documents directory before: %@", [fileManager contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:&error]);

but it crash after

"NSLog(@"file exist and I delete it");" 

and I have only a "lldb" in consolle.

Upvotes: 1

Views: 4351

Answers (3)

Ravi Raman
Ravi Raman

Reputation: 1070

Try the following:

-(BOOL) removePDF
{
    BOOL removeStatus = NO;

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                               NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    NSString* fileName = @"file.pdf"; //your file here..

    NSString* filePath = [NSString stringWithFormat:@"%@/%@", docsDir, fileName];

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath] == YES)
    {
       removeStatus = [[NSFileManager defaultManager] removeItemAtPath:filePath]; 
    }
    return removeStatus;
}

Upvotes: 0

Rad'Val
Rad'Val

Reputation: 9231

Most probably you don't remove the file before downloading a new one and ASIHttpRequest sees that there's already a file with the same name and appends data to it instead of replacing the file. I'm not sure about the PDF format, but that shouldn't normally result in a merged readable file. In any case, first you need to use the error mechanism that the filemanager class offers you. Is bad to pass NULL. Very bad. So, create a NSError object and pass it to the contentsOfDirectoryAtPath and removeItemAtPath methods, then check the error, be sure the operations are done successfully. After that, you may want to check the extension upper case as well, as Unix based systems are case sensitive (although the simulator is not, the device is) and a example.PDF file will not get deleted based on your code.

Upvotes: 0

tkanzakic
tkanzakic

Reputation: 5499

I use this method to delete pdf files from a local cache, with a few modifications you can adapt it to your necessities

- (void)removePDFFiles
{
    NSFileManager *fileMngr = [NSFileManager defaultManager];

    NSArray *cacheFiles = [fileMngr contentsOfDirectoryAtPath:[self cacheDirectory]
                                                    error:nil];
    for (NSString *filename in cacheFiles) {
        if ([[[filename pathExtension] lowercaseString] isEqualToString:@"pdf"]) {
            [fileMngr removeItemAtPath:[NSString stringWithFormat:@"%@/%@", [self cacheDirectory], filename] error:nil];
        }
    }
}

Upvotes: 3

Related Questions