2

I'm trying to programmatically create a folder with Cocoa.

I've written an NSString category and we've got the following function there :

- (void)createAsFolder
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError* err = nil;

    [fileManager createDirectoryAtPath:self withIntermediateDirectories:YES attributes:nil error:&err];

    if (err)
    {
        NSLog(@"ERROR : %@",err);
    }
}

So, in a few words, let's say we have an NSString* path = @"/some/path/is/here";, we can create it simply by :

[path createAsFolder];

The thing is, although it works PERFECTLY for normal folders, it does NOT when the path specified is a bundle (that is : WITH an extension). E.g.

NSString* path = @"/this/is/a/path/to/some/bundle.bun";

[path createAsFolder];

The above does NOT work.

Any ideas on how to fix that?

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • @EvanMulawski Nope, absolutely NO error (and absolutely NO bundle/folder in place where it should be); I've check all paths through logging - they are correct. I'm basically copying a whole file-tree structure from one location to another. And everything copies great, EXCEPT for the bundles (and their contents). – Dr.Kameleon Apr 14 '12 at 23:58

2 Answers2

3

OK, here's the answer (thanks to @thundersteele), if you want to copy a full file tree from on place to another :

NSFileWrapper* w = [[NSFileWrapper alloc] initWithPath:initialPath];

[w writeToFile:destinationPath atomically:YES updateFilenames:YES];

And yep : it has ABSOLUTELY no problem whether the subfolders are packages/bundles or whatever. Not that hard, huh? Just 2 lines... lol

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 1
    That method is depreciated as of OS X v10.6, you should use writeToURL:options:originalContentsURL:error: instead. – rdelmar Apr 15 '12 at 05:11
2

Try NSFileWrapper instead. I think it can do what you want to do.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSFileWrapper_Class/Reference/Reference.html

thundersteele
  • 673
  • 3
  • 5
  • I've studied `NSFileWrapper` like 10 times and dismissed it as a possible solution. Your telling me to recheck, made me reconsider... Yep, you were 100% right. I've written one function for the Directory trees, one for copying a file, one for creating a folder (which did not fully work, since it stumbled on packages/bundles), and one for traversing the whole tree and create the subnodes, while all this could be done with 2 lousy lines... Some times I feel kinda silly... Thanks a lot, buddy! :-) – Dr.Kameleon Apr 15 '12 at 00:21
  • It was really a shot in the dark. I read about it a few days ago when studying document based application design, and I just thought that it might work ;) – thundersteele Apr 15 '12 at 00:23