Lens c, validation file exists on path

I created a screen using Objective-C in an iPhone project. In this case, there are two buttons (for example, A and B). When you click A, one xml file will be created in the folder (for example, INBOX).

My problem: I need to create a file only if it does not exist in the INBOX folder. How can i do this? Can someone tell me the syntax?

+49
objective-c iphone
Feb 22 2018-11-22T00:
source share
3 answers

Can you check the NSFileManager.

NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *pathForFile; if ([fileManager fileExistsAtPath:pathForFile]){ } 
+122
Feb 22 '11 at 7:53
source share

There were similar questions with SO:

  • This answer is probably most suitable for you.

Link to NSFileManagerAPI

+1
Feb 22 '11 at 7:55
source share

try the following:

 - (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; } 
+1
Feb 22 '11 at 8:22
source share



All Articles