Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a file in subfolder of document directory?

I am creating one folder in document directory with name newFolder.So now i want to know how to create txt file in my folder which is create with newFolder i now how to create file in document directory but how to create file in my folder which is create by me.I want to fetch this file also from newFolder and pass to mail composer in iPhone.

This is create folder in my document folder but now i want create file in this folder name newFolder help me in this.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"newFolder"];
    NSError *error = NULL;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

i write this code but my csv file is create in doument diretory folder i want to cretae this file in my folder which is doument directory have with name newFolder

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

        NSString *filename = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"EmployeeBase.Csv"]];
            NSError *error = NULL;
            BOOL written = [csvString writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error];
            if (!written)
                NSLog(@"write failed, error=%@", error);
like image 343
Rocky Avatar asked Jun 07 '12 05:06

Rocky


People also ask

How do I move a file into a subfolder?

Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want. If you need to, click on any folder you see to access its subfolders.

What is the difference between a folder and a subfolder?

Answer: is that subfolder is (computing) a folder within another folder while folder is (computing) a virtual container in a computer's file system, in which files and other folders may be stored the files and subfolders in a folder are usually related.


2 Answers

Here is an example of saving image in documents directory in your own folder...

UIImage *imageForShare = [UIImage imageNamed:@"anyImage.jpg"];
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
NSData *data = UIImageJPEGRepresentation(imageForShare, 1.0);
[data writeToFile:fileName atomically:YES];

Swift 4.0 Version

guard let image = UIImage(named: "anyImage.jpg") else {return}
guard var stringPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first else {return}
stringPath = stringPath + "New Folder"

// New Folder is your folder name
if !FileManager.default.fileExists(atPath: stringPath) {
      do {
          try FileManager.default.createDirectory(atPath: stringPath, withIntermediateDirectories: false, attributes: nil)
      } catch let error {
          print(error.localizedDescription)
          return
      }
 }

 let fileName = stringPath + "/image.jpg"
 let url = URL(fileURLWithPath: fileName)
 if let data = UIImageJPEGRepresentation(image, 1.0) {
      do {
            try data.write(to: url)
        } catch let error {
            print(error.localizedDescription)
       }
 }
like image 101
TheTiger Avatar answered Sep 24 '22 01:09

TheTiger


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,    
    YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *myPath = [documentsPath stringByAppendingPathComponent:@"/MyDocuments"];

    //Create folder
    if (![[NSFileManager defaultManager] fileExistsAtPath:myPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:myPath withIntermediateDirectories:NO 
    attributes:nil error:NULL];

    NSString *newStr=[[NSString alloc] initWithFormat:@"Sample.txt"];
    NSString *filePath = [myPath stringByAppendingPathComponent:newStr]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file
like image 38
SURESH SANKE Avatar answered Sep 22 '22 01:09

SURESH SANKE