Logger
Logger

Reputation: 1286

How to store files in folder which is created by using “documentDirectory” swift

I am creating new folder using createDirectory with below code.

    *let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    // Get documents folder
    let documentsDirectory: String = paths.first ?? ""
    // Get your folder path
    let dataPath = documentsDirectory + "/MyNewFolder"
    print("Path\(dataPath)")
    if !FileManager.default.fileExists(atPath: dataPath) {
        // Creates that folder if no exists
        try? FileManager.default.createDirectory(atPath: dataPath, withIntermediateDirectories: false, attributes: nil)
    }*

Now i want to store new file like log.text under “MyNewFolder”. Can anybody suggest me how to save new files under “MyNewFolder” folder

Thanks in advance.

Upvotes: 2

Views: 4357

Answers (3)

vadian
vadian

Reputation: 285059

NSSearchPathForDirectoriesInDomains is outdated. The recommended API is the URL related API of FileManager

let folderName = "MyNewFolder"
let fileManager = FileManager.default
let documentsFolder = try! fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let folderURL = documentsFolder.appendingPathComponent(folderName)
let folderExists = (try? folderURL.checkResourceIsReachable()) ?? false
do {
    if !folderExists {
        try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: false)
    }
    let fileURL = folderURL.appendingPathComponent("log.txt")
    let hello = Data("hello".utf8)
    try hello.write(to: fileURL)
    
} catch { print(error) }

You are strongly discouraged from building paths by concatenating strings.

Update:

In iOS 16+, macOS 13+ Apple moved the APIs to get the URLs of the standard folders into URL. appendingPathComponent has also been changed to be able to tell the compiler that the path represents a directory

let folderName = "MyNewFolder"
let folderURL = URL.documentsDirectory.appending(path: folderName, directoryHint: .isDirectory)
let folderExists = (try? folderURL.checkResourceIsReachable()) ?? false
do {
    if !folderExists {
        try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: false)
    }
    let fileURL = folderURL.appendingPathComponent("log.txt")
    let hello = Data("hello".utf8)
    try hello.write(to: fileURL)
    
} catch { print(error) }

Upvotes: 11

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can try

try? FileManager.default.createDirectory(atPath: dataPath, withIntermediateDirectories: false, attributes: nil)

do { 

    let sto =  URL(fileURLWithPath: dataPath + "log.txt")  // or let sto =  URL(fileURLWithPath: dataPath + "/log.txt") 

    try Data("SomeValue".utf8).write(to: sto)

    let read = try Data(contentsOf: sto)

    print(String(data: read, encoding: .utf8)!)
}
catch {

    print(error)
}

Upvotes: 3

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

let filePath = dataPath + "/log.txt"
FileManager.default.createFile(filePath, contents:dataWithFileContents, attributes:nil)

Upvotes: 1

Related Questions