Skip to content

Instantly share code, notes, and snippets.

@andrewwoz
andrewwoz / set_get_attributes_of_file.swift
Created October 16, 2019 16:52
[Setting and getting attributes of file in swift] #fs
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
}
@andrewwoz
andrewwoz / file_access.swift
Created October 16, 2019 16:45
[Checking file access in swift] #fs
_ = fs.fileExists(atPath: filePath)
_ = fs.isReadableFile(atPath: filePath)
_ = fs.isWritableFile(atPath: filePath)
_ = fs.isDeletableFile(atPath: filePath)
_ = fs.isExecutableFile(atPath: filePath)
@andrewwoz
andrewwoz / symbolik_links.swift
Last active October 16, 2019 16:39
[Creating symbolik links on files using swift] #fs
@andrewwoz
andrewwoz / creating_hard_links.swift
Last active October 16, 2019 16:24
[Creating hard links to file in swift] #fs
@andrewwoz
andrewwoz / copy_file.swift
Last active October 16, 2019 15:16
[Copying files] #fs
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)
@andrewwoz
andrewwoz / replace_item.swift
Created October 16, 2019 14:31
[Replace item(file)] #fs
let fs = FileManager.default
let docsURL = ...
// create first test file
let newFile = docs.appendingPathComponent("newFile.txt")
fs.createFile(
atPath: newFile.path,
contents: ...,
attributes: nil)
@andrewwoz
andrewwoz / rm_file.swift
Last active October 15, 2019 06:06
[Remove item from file system in swift] #fs
let fs = FileManager.default
guard let documents = fs.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
try? fs.removeItem(at: documents.appendingPathComponent("hello.txt"))
@andrewwoz
andrewwoz / make_file.swift
Last active October 16, 2019 15:17
[Create file] #fs
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
}
@andrewwoz
andrewwoz / create_dir.swift
Last active October 15, 2019 06:07
[Create directory in swift] #fs
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
}
@andrewwoz
andrewwoz / deep_enum_directory.swift
Last active October 15, 2019 06:05
[Perform deep enumeration of directory] #fs
// 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)