Search code examples
iosobjective-cxcode

How do I know whether a directory exists or not inside my Xcode project using Objective-C?


I have created a directory inside my project using this code

NSError * error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/createDirectory1"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
{
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
    NSLog(@"Create a new Dir");
}
else 
{
    NSLog(@"Dir already exist");
}

now I want to check whether the directory exists or not? How can I get that through Objective-C code?


Solution

  •     NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filePath=[NSString stringWithFormat:@"%@/createDirectory1", docPath];
        BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
        if (!fileExists)
        {
    
        }
        else
        {
           // Your Code
        }