0

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.

Krunal
  • 6,266
  • 20
  • 89
  • 154
  • 1
    possible duplicate of [How to check if a file exists in Documents folder?](http://stackoverflow.com/questions/1638834/how-to-check-if-a-file-exists-in-documents-folder) – Michael Dautermann Aug 01 '12 at 07:21

3 Answers3

3

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");

    }
Shamsudheen TK
  • 28,767
  • 9
  • 64
  • 96
2

you also can try..

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"file"];
success=[filemanager fileExistsAtPath:path];
kushalrshah
  • 628
  • 5
  • 14
  • check my `DestPath` there is a folder inside Document folder i want to check `Myfile.epub` is present inside `DestPath` or not. – Krunal Aug 01 '12 at 07:31
0
 //Get the Document directory path 

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
 NSString *documentsPath = [paths objectAtIndex:0]; 
 documentsPath = [documentsPath stringByAppendingPathComponent:@"Myfile.epub"];
 if (![[NSFileManager defaultManager] fileExistsAtPath:documentsPath]) 
 {
    //file not exits in Document Directories
 }
  else
  {
    //file exist in Document Directories
  }
Bond
  • 328
  • 4
  • 16