This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
let docsURL = ... | |
let filePath = docsURL.appendingPathComponent("article.txt").path | |
// get attributes of file | |
guard let attributes = try? fs.attributesOfItem(atPath: filePath) else { | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = fs.fileExists(atPath: filePath) | |
_ = fs.isReadableFile(atPath: filePath) | |
_ = fs.isWritableFile(atPath: filePath) | |
_ = fs.isDeletableFile(atPath: filePath) | |
_ = fs.isExecutableFile(atPath: filePath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
let docs = ... | |
let articleURL = docs.appendingPathComponent("article.txt") | |
let linkURL = docs.appendingPathComponent("article.link.txt") | |
do { | |
try fs.createSymbolicLink(at: linkURL, withDestinationURL: articleURL) | |
} catch let error { | |
print(error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
let docsURL = ... | |
let articleURL = docsURL.appendingPathComponent("article.txt") | |
let linkedArticle = docsURL.appendingPathComponent("link_article.txt") | |
try? fs.linkItem(at: articleURL, to: linkedArticle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
let fileURL = docsURL.appendingPathComponent("file.txt") | |
fs.createFile( | |
atPath: fileURL.path, | |
contents: ..., | |
attributes: nil) | |
let copyURL = docsURL.appendingPathComponent("file-copy.txt") | |
try? fs.copyItem(at: fileURL, to: copyURL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
let docsURL = ... | |
// create first test file | |
let newFile = docs.appendingPathComponent("newFile.txt") | |
fs.createFile( | |
atPath: newFile.path, | |
contents: ..., | |
attributes: nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
guard let documents = fs.urls(for: .documentDirectory, in: .userDomainMask).first else { | |
return | |
} | |
try? fs.removeItem(at: documents.appendingPathComponent("hello.txt")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
let content = "Hello World" | |
let data = content.data(using: .utf8) | |
let isCreated = fs.createFile( | |
atPath: documents.appendingPathComponent("hello.txt").path, | |
contents: data, attributes: nil) | |
if isCreated { | |
// file was created | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fs = FileManager.default | |
let baseURL: URL = ... | |
do { | |
try fs.createDirectory( | |
at: baseURL.appendingPathComponent("someDirectory/someSubDirectory"), | |
withIntermediateDirectories: true, attributes: nil) | |
} catch let error { | |
/// handle error | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Performs a deep enumeration of the specified directory and returns the paths of all of the contained subdirectories. | |
guard let documents = try? fs.url(for: .documentDirectory, | |
in: .userDomainMask, | |
appropriateFor: nil, | |
create: false) else { return } | |
let subpaths = try! fs.subpathsOfDirectory(atPath: documents.path) |
NewerOlder