Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create directory programmatically

I want to create a directory inside my applicationSupportDirectory. My understanding is that the applicationSupportDirectory does not allow users to see the data within. This is why I have chosen it. However, the code I am using below seems to fail and I am not sure why.

Can anyone tell me what I have done wrong? Thanks!

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,   NSUserDomainMask, YES);
NSString *applicationDirectory = [paths objectAtIndex:0]; // Get directory
NSString *dataPath = [applicationDirectory stringByAppendingPathComponent:@"drawings"];

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

NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])
  ;// success
else
{
  NSLog(@"Failed");
}
}
}
like image 837
Alex G Avatar asked May 04 '13 04:05

Alex G


People also ask

How do I create a directory in Java?

In Java, the mkdir() function is used to create a new directory. This method takes the abstract pathname as a parameter and is defined in the Java File class. mkdir() returns true if the directory is created successfully; else, it returns false​.

How do you create a directory in C?

The mkdir() function creates a new, empty directory with name filename.

How does mkdir work in C?

The mkdir function creates a new, empty directory with name filename . The argument mode specifies the file permissions for the new directory file. See The Mode Bits for Access Permission, for more information about this. Write permission is denied for the parent directory in which the new directory is to be added.


1 Answers

Try this one, it works for me.

NSString* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask,
                                                              YES)[0];
NSString *folder = [documentsPath stringByAppendingPathComponent:@"foldername"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if (![fileManager fileExistsAtPath:folder]){
    [fileManager createDirectoryAtPath:folder
           withIntermediateDirectories:YES
                            attributes:nil
                                 error:&error];
}
like image 132
dhaya Avatar answered Sep 18 '22 02:09

dhaya