Nsfilemanager Fileexistsatpath:Isdirectory and Swift

How check directory exist?

You can do smarter solution Like that without Two function completion(isExit,directoryURL) :

And simple use it in one line :

 self.createSystemFolders("json") { (isExit, url) in
print(isExit)
print(url)
}

CreateSystemFolders:

 func createSystemFolders(_ folderName:String ,_ completion:(_ isExit:Bool?,_ directoryURL:URL?) -> Void){
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
let directory = paths[0]
let fileManager = FileManager.default

let url = URL(fileURLWithPath: directory).appendingPathComponent(folderName)


if !fileManager.fileExists(atPath: url.path) {
do {
try fileManager.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
completion(false,url)
}
catch {
print("Error: Unable to create directory: \(error)")
completion(nil,nil)
}
var url = URL(fileURLWithPath: directory)
var values = URLResourceValues()
values.isExcludedFromBackup = true
do {
try url.setResourceValues(values)
completion(false,url)
}
catch {
print("Error: Unable to exclude directory from backup: \(error)")
completion(nil,nil)
}
}else{
completion(true,url)
}
}

Check if path is a directory in Swift2?

You can use an overload of fileExistsAtPath that tells you that path represents a directory:

var isDir : ObjCBool = false
let path = ...
let fileManager = FileManager.default
if fileManager.fileExists(atPath: path, isDirectory:&isDir) {
print(isDir.boolValue ? "Directory exists" : "File exists")
} else {
print("File does not exist")
}

In Swift fileExistsAtPath(_ path: String, isDirectory isDirectory: UnsafeMutablePointerObjCBool) - Bool accepts single parameter only

Some might find this a little neater. This is Swift 3.

var directory: ObjCBool = ObjCBool(false)
var exists: Bool = FileManager.default.fileExists(atPath: "…", isDirectory: &directory)

if exists && directory.boolValue {
// Exists. Directory.
} else if exists {
// Exists.
}

Check if NSURL is a directory

that is working quote well on my side:

var error: NSError?
let documentURL : NSURL = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true, error: &error)
var isDirectory: ObjCBool = ObjCBool(0)
if NSFileManager.defaultManager().fileExistsAtPath(documentURL.path, isDirectory: &isDirectory) {
println(isDirectory)
}

NOTE: it checks whether the Documents folder is a folder. you can replace the URL with anything, of course.

Determine a directory is a package in swift

There’s a Cocoa method, isFilePackageAtPath, on NSWorkspace for that.

import Cocoa

let sw = NSWorkspace.sharedWorkspace()

if sw.isFilePackageAtPath("/Applications/Xcode.app") {
// True, so this will execute.
}
if sw.isFilePackageAtPath("/usr/bin") {
// False, so this won't execute.
}

NSFileManager.defaultManager().fileExistsAtPath returns false instead of true

(The code in this answer has been updated for Swift 3 and later.)

Apparently your path variable is a NSURL (describing a file path). To get the path as
a string, use the path property, not absoluteString:

let exists = FileManager.default.fileExists(atPath: path.path)

absoluteString returns the URL in a string format, including
the file: scheme etc.

Example:

let url = URL(fileURLWithPath: "/path/to/foo.txt")

// This is what you did:
print(url.absoluteString)
// Output: file:///path/to/foo.txt

// This is what you want:
print(url.path)
// Output: /path/to/foo.txt


Related Topics



Leave a reply



Submit