Take the 2-minute tour ×
- (NSURL *) applicationDocumentsDirectory


NSURL *folder = [[[Database sharedInstance] applicationDocumentsDirectory] URLByAppendingPathComponent:@"temp"];
BOOL isFolder;
NSLog(@"URL: %@", folder);
if ([[NSFileManager defaultManager] fileExistsAtPath:folder.absoluteString isDirectory:&isFolder] == NO)
    NSLog(@"IsFolder: %d", isFolder);
    [[NSFileManager defaultManager] createDirectoryAtURL:folder withIntermediateDirectories:YES attributes:nil error:&error];
    if (error)

I run this part of code twice.

I expect that the folder will be created, but every time I receive a log: "IsFolder: 7".

Why the folder wasn't created after the first run?

share|improve this question
make sure that isFolder is true –  Dhara Jan 25 '13 at 8:42
I have changed to: "BOOL isFolder = YES". fileExistsAtPath still returns NO... –  iWheelBuy Jan 25 '13 at 8:47
try to use the ...AtPath: instead of the ...AtURL:, it might help you. –  holex Jan 25 '13 at 8:53
add comment

2 Answers

up vote 2 down vote accepted

please replace folder.absoluteString to folder, your problem solve

follow setp by setp

1)Get documents folder

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0]; 

2)Get documents folder and folder name

NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"];

3) check alerdy exists or not

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){

    NSError* error;
    if(  [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])

            NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
            NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
share|improve this answer
Okay. I have changed withIntermediateDirectories: from YES to NO. On the second run I receive "The operation couldn’t be completed. File exists". It means that the file is created. I wonder why fileExistsAtPath returns NO each time?! –  iWheelBuy Jan 25 '13 at 8:42
check my update answer –  Deepesh Jan 25 '13 at 9:23
add comment

The problem was in folder.absoluteString.

It should be folder.path instead.

share|improve this answer
add comment

Your Answer


