45

I've found this way of creating a directory if it does not exist. But it looks a bit wonky and I am afraid that this can go wrong in 1 of 1000 attempts.

if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) {
    [[NSFileManager defaultManager] createDirectoryAtPath:bundlePath withIntermediateDirectories:YES attributes:nil error:NULL];
}

There is only this awkward method fileExistsAtPath which also looks for files and not only directories. But for me, the dangerous thing is: What if this goes wrong? What shall I do? What is best practice to guarantee that the directory is created, and only created when it does not exist?

I know file system operations are never safe. Device could loose battery power suddenly just in the moment where it began shoveling the bits from A to B. Or it can stumble upon a bad bit and hang for a second. Maybe in some seldom cases it returns YES even if there is no directory. Simply put: I don't trust file system operations.

How can I make this absolutely safe?

Meet Doshi
  • 3,983
  • 10
  • 35
  • 76
dontWatchMyProfile
  • 42,456
  • 49
  • 169
  • 255

6 Answers6

88

You can actually skip the if, even though Apple's docs say that the directory must not exist, that is only true if you are passing withIntermediateDirectories:NO

That puts it down to one call. The next step is to capture any errors:

NSError * error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:bundlePath
                          withIntermediateDirectories:YES
                                           attributes:nil
                                                error:&error];
if (error != nil) {
    NSLog(@"error creating directory: %@", error);
    //..
}

This will not result in an error if the directory already exists.

e.James
  • 109,080
  • 38
  • 170
  • 208
  • Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?) – dontWatchMyProfile May 25 '11 at 15:00
  • I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one? – e.James May 25 '11 at 15:17
  • 1
    @Sven: I don't see how it makes a difference to the logic? – e.James Feb 10 '13 at 14:53
  • Cocoa error handling generally does not promise that the error pointer will be `NULL` on success, only that it will be populated on failure. – jscs Oct 21 '17 at 19:56
9

For Swift 3.0

do {
    try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
    print(error)
}
Sergey Nikitin
  • 763
  • 7
  • 22
9

Swift 4.2

let fileManager = FileManager.default
let documentsURL =  fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!

let imagesPath = documentsURL.appendingPathComponent("Images")
do
{
    try FileManager.default.createDirectory(atPath: imagesPath.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
    NSLog("Unable to create directory \(error.debugDescription)")
}
Abhishek Jain
  • 4,155
  • 2
  • 27
  • 29
3
NSFileManager *fileManager= [NSFileManager defaultManager]; 
if(![fileManager fileExistsAtPath:directory isDirectory:&isDir])
    if(![fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL])
        NSLog(@"Error: Create folder failed %@", directory);

From an SO topic here.

After creating a directory, you can flush the file system then check to see if your newly created directory exists. This is probably overkill, but you can never have too much overkill.

Community
  • 1
  • 1
Charles Burns
  • 9,631
  • 7
  • 58
  • 77
  • What do you mean by "flush the file system"? – dontWatchMyProfile May 25 '11 at 15:02
  • dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: http://stackoverflow.com/questions/459537/clear-buffer-cache-on-mac-os-x – Charles Burns May 25 '11 at 16:58
  • 2
    Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing. – James Moore Nov 29 '12 at 18:48
3

Swift 5.0

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let docURL = URL(string: documentsDirectory)!
let dataPath = docURL.appendingPathComponent("MyFolder")
if !FileManager.default.fileExists(atPath: dataPath.absoluteString) {
    do {
        try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
    } catch {
        print(error.localizedDescription);
    }
}
Ved Sharma
  • 571
  • 3
  • 17
1

In swift 2 it looks like this:

do {
    try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
    print(error)
}
Sergey Nikitin
  • 763
  • 7
  • 22
Chris
  • 2,737
  • 2
  • 25
  • 27