objective c,checking file exist at path

IphoneObjective C

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

Can you Please Check NSFileManager.

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *pathForFile;

if ([fileManager fileExistsAtPath:pathForFile]){ 
		
}

Solution 2 - Iphone

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

Solution 3 - Iphone

There have been similar questions on SO before:

  1. This answer is probably most appropriate for you

Link to the NSFileManagerAPI

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSowmyaView Question on Stackoverflow
Solution 1 - IphonePgmFreekView Answer on Stackoverflow
Solution 2 - IphonemeronixView Answer on Stackoverflow
Solution 3 - IphoneOzair KafrayView Answer on Stackoverflow