Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective c,checking file exist at path

I have created a screen using Objective-C in an iPhone project. In that, there are 2 buttons (say A and B). On the click of button A, one xml file will be created in a folder (say INBOX).

My problem is, I need to create the file only if it does not exist at folder INBOX. How can I do this? Can any one tell me the syntax?

like image 948
Sowmya Avatar asked Feb 22 '11 07:02

Sowmya


2 Answers

Can you Please Check NSFileManager.

NSFileManager *fileManager = [NSFileManager defaultManager];  NSString *pathForFile;  if ([fileManager fileExistsAtPath:pathForFile]){   } 
like image 55
PgmFreek Avatar answered Sep 22 '22 11:09

PgmFreek


try this:

- (BOOL)checkAndCopy {     NSError **error;     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString *documentsDirectory = [paths objectAtIndex:0];     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourFile.xml"];       NSFileManager *fileManager = [NSFileManager defaultManager];      if (![fileManager fileExistsAtPath: path])     {         // this copy an existing xml (empty?) file from main bundle:         // try else to create a new file          NSString *bundle =  [[ NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"xml"];         [fileManager copyItemAtPath:bundle toPath:path error:error];         return YES;     }     return NO; } 
like image 27
meronix Avatar answered Sep 22 '22 11:09

meronix