6

I want to know how to check if a directory exists in my application

for example: if I want to search if a folder exists in my application document

and how to create a new folder within it

best regards

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Ali
  • 1,975
  • 5
  • 36
  • 55

1 Answers1

19

Checking for file existence:

+(BOOL)fileExistsAtAbsolutePath:(NSString*)filename {
    BOOL isDirectory;
    BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:filename isDirectory:&isDirectory];

    return fileExistsAtPath && !isDirectory;
}

Checking for directory existence:

+(BOOL)directoryExistsAtAbsolutePath:(NSString*)filename {
    BOOL isDirectory;
    BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:filename isDirectory:&isDirectory];

    return fileExistsAtPath && isDirectory;
}
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82