11

I am using the following line of code to save my file of yoyo.txt in the Documents folder ::

NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"docDir is yoyo :: %@", docDir);
NSString *FilePath = [docDir stringByAppendingPathComponent:@"yoyo.txt"];

However, I wish to save my file in a folder of yoyo i.e. inside the Documents folder i.e. I want to create another folder named as "yoyo" and then save my file of yoyo.txt into it. How can I do that ?? Thanks.

kamalbhai
  • 510
  • 2
  • 10
  • 23
  • Xcode is the IDE. The language is Objective C, you can write the same code in an text editor and compile it on the command line. It's a common mistake though, to mix up `Xcode` and `Objective C`. – Julian F. Weinert Apr 24 '14 at 11:03

3 Answers3

16

Here is a sample code (assume manager is [NSFileManager defaultManager]):

BOOL isDirectory;
NSString *yoyoDir = [docDir stringByAppendingPathComponent:@"yoyo"];
if (![manager fileExistsAtPath:yoyoDir isDirectory:&isDirectory] || !isDirectory) {
        NSError *error = nil;
        NSDictionary *attr = [NSDictionary dictionaryWithObject:NSFileProtectionComplete
                                                         forKey:NSFileProtectionKey];
        [manager createDirectoryAtPath:yoyoDir
           withIntermediateDirectories:YES
                            attributes:attr
                                 error:&error];
        if (error)
            NSLog(@"Error creating directory path: %@", [error localizedDescription]);
    }
graver
  • 15,183
  • 4
  • 46
  • 62
  • what is "isDictionary" in your code. Also what is the purpose of NSDictionary which you have declared in your code ?? – kamalbhai Jun 25 '12 at 10:45
  • isDictionary is BOOL, used to check if the file that exists in the path is directory, because there could be a file which is not a directory... The dictionary is used to pass attributes for the newly created directory in the code I have posted this is `The file is stored in an encrypted format on disk and cannot be read from or written to while the device is locked or booting.` you can pass `nil` to this. You may also see [NSFileManager Class Reference](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsfilemanager_Class/Reference/Reference.html) – graver Jun 25 '12 at 10:50
11
+(void)createDirForImage :(NSString *)dirName
{
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }

}
Mani
  • 1,841
  • 15
  • 29
2

Here dataPath will be the final path for saving your file

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

        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
    }
    dataPath = [dataPath stringByAppendingPathComponent:@"/yoyo.txt"];
Amulya
  • 182
  • 1
  • 12