How to check if a file exists on a specific user path?

I have a demo.png file that is saved in a folder documents/test/. Now I want to check if files exist on this particular path, and I want to delete this file. How to check what you can tell me?

+5
source share
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);
    }
   } 
+9
source
if([[NSFileManager defaultManager] fileExistsAtPath:somePath])
     [[NSFileManager defaultManager] removeItemAtPath:somePath error:NULL]; 

This can be used to check if a file exists in your path.

+5
source

All Articles