Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a file exists at user defined path?

I am having a file called demo.png which is saved in the documents/test/ folder. Now I want to check whether the files exists at that particular path and I want to delete that file. How to check that can you please tell me ??

like image 746
Perseus Avatar asked Apr 05 '12 07:04

Perseus


2 Answers

 NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 


 NSString* foofile = [documentsPath stringByAppendingPathComponent:@"foo.html"];
 BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];


 if (fileExists)    //Does file exist?
     {
    if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error])   //Delete it
    {
        NSLog(@"Delete file error: %@", error);
    }
   } 
like image 174
maheswaran Avatar answered Oct 11 '22 19:10

maheswaran


if([[NSFileManager defaultManager] fileExistsAtPath:somePath])
     [[NSFileManager defaultManager] removeItemAtPath:somePath error:NULL]; 

This can be used to check if file exist at your path.

like image 36
DivineDesert Avatar answered Oct 11 '22 18:10

DivineDesert