Krunal
Krunal

Reputation: 6490

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.

Upvotes: 0

Views: 1946

Answers (3)

Shamsudheen TK
Shamsudheen TK

Reputation: 31339

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

    }

Upvotes: 3

Bond
Bond

Reputation: 328

 //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
  }

Upvotes: 0

kushalrshah
kushalrshah

Reputation: 648

you also can try..

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"file"];
success=[filemanager fileExistsAtPath:path];

Upvotes: 2

Related Questions