Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How check directory exist?

Tags:

ios

swift

I have functions to create directorys:

func createSystemFolders(){
        // Create a FileManager instance
        let fileManager = FileManager.default
        do {
            try fileManager.createDirectory(atPath: "json", withIntermediateDirectories: true, attributes: nil)
        }
        catch let error as NSError {
            debugPrint("\(ErrorsLabels.AppDelegate01): \(error)")
        }

        do {
            try fileManager.createDirectory(atPath: "inspirations", withIntermediateDirectories: true, attributes: nil)
        }
        catch let error as NSError {
            debugPrint("\(ErrorsLabels.AppDelegate02): \(error)")
        }

        do {
            try fileManager.createDirectory(atPath: "products", withIntermediateDirectories: true, attributes: nil)
        }
        catch let error as NSError {
            debugPrint("\(ErrorsLabels.AppDelegate03): \(error)")
        }
    }

I need second function to check directory exist.

Haw can I check it?

like image 291
Łukasz Betta Avatar asked Jul 13 '26 06:07

Łukasz Betta


1 Answers

You can use this,

fileprivate func directoryExistsAtPath(_ path: String) -> Bool {
    var isDirectory : ObjCBool = true
    let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
    return exists && isDirectory.boolValue
}
like image 174
PPL Avatar answered Jul 15 '26 20:07

PPL