Search code examples
objective-ccocoansfilemanager

CreateDirectoryAtPath: doesn't work


I have simple code:

NSFileManager *fileManager = [NSFileManager defaultManager];

if (!fileManager) NSLog(@"Manager doesn't exist!");

if(![fileManager fileExistsAtPath:destString]) {
    if (![fileManager createDirectoryAtPath:destString withIntermediateDirectories:YES attributes:nil error:&error]){
        NSLog(@"%@", [error localizedFailureReason]);
    }
}
else NSLog(@"Exists!");

vars:

destString = file://localhost/Users/SOMEUSER/Desktop/NEWFOLDER/

When I am trying to create a folder then program writes "Exists" but on desktop doesn't exist. When I remove fileExistsAtPath: then there's no error but no directory too. Thx 4 reply!


Solution

  • -createDirectoryAtPath:withIntermediateDirectories:attributes:error: takes the path to create as a UNIX-style path string, and not as a file URL-style string. That is, you want to pass it a string like /Users/SOMEUSER/Desktop/NEWFOLDER/.

    Alternatively, if you're dealing with a URL-style string then you could switch to using -createDirectoryAtURL:withIntermediateDirectories:attributes:error: instead and construct an NSURL from your string.