Search code examples
iphoneipadnsbundlefile-existsnsdocumentdirectory

Check file exists in a folder inside Directory in iPhone


I am new to iPhone,

I want to check whether Myfile exists in folder inside DocumentDirectory ?

For eg:

Myfile.epub is one of my file and i want to check whether this file exists at my DestPath or not ?

DestPath is my DocumentDirectory path.

DestPath=/Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/D414BC19-C005-4D93-896D-A6FB71DE4D21/Documents/Derivatives

Any help will be appreciated.


Solution

  • This solution worked for me..
    You don't want to give the entire part of your path like /Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/D414BC19-C005-4D93-896D-A6FB71DE4D21/Documents/Derivatives

    you have to give Derivatives/Myfile.epub (FolderName/filename) for checking the file path

        NSFileManager *filemanager=[NSFileManager defaultManager];  
    
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory= [paths objectAtIndex:0];
    
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Derivatives/Myfile.epub"];
    
        BOOL success =[filemanager fileExistsAtPath:path];
    
        if (success == YES) {   
    
            NSLog(@"exists");
        }
        else {
    
           NSLog(@"not exists");
    
        }