4

I have some files located at my document folder. I am saving the files like "data_20201223163209.pdf", "data_20201223171831.pdf", "data_20201222171831.pdf", "data_20201221171831.pdf" etc. Now I want to replace "data" with other strings like "newdata". So my files should be "newdata_20201223163209.pdf", "newdata_20201223171831.pdf", "newdata_20201222171831.pdf", "newdata_20201221171831.pdf"

My code:

do {
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let documentDirectory = URL(fileURLWithPath: path)
    let originPath = documentDirectory.appendingPathComponent("data")
    let destinationPath = documentDirectory.appendingPathComponent("newdata")
    try FileManager.default.moveItem(at: originPath, to: destinationPath)
} catch {
    print(error)
}

Please help me resolve this issue

2
  • "to resolve this issue" What is the issue? You aren't allowed to access files there without user's consent. How are you accessing them? Commented Dec 24, 2020 at 2:39
  • And what is the NSDocument tag for? Commented Dec 24, 2020 at 2:40

1 Answer 1

3

You just need to get the contents of your directory, filter the urls which names starts with "data_", iterate those urls and rename each one moving it to the same directory with the new name. Note that this assumes there is no file at the destination with the new names.

// Get the documents url
let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
    // Get its contents
    let contents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil)
    print(contents)
    // filter the contents that starts with "data_"
    let dataFiles = contents.filter { $0.lastPathComponent.hasPrefix("data_") }
    // iterate the source files
    for srcURL in dataFiles {
        // create the destinations appending "newdata_" + the source lastPathComponent dropping its "data_" prefix
        let dstURL = documentsUrl.appendingPathComponent("newdata_" + srcURL.lastPathComponent.dropFirst(5))
        // move/rename your files
        try FileManager.default.moveItem(at: srcURL, to: dstURL)
    }
} catch {
    print(error)
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Leo thanks for your answer. when we are checking the lastPathComponent , the "pdf" is the last path compnet here. so it has prefix like :- "data_20201223163209.", "data_20201223171831.", "data_20201222171831." etc. so it is not entering to the for loop.
Pdf is the path extension
Are you using macOS or iOS? If you are using macOS you need to disable sandbox capability
Thanks a lot @Leo Dabus. My code is working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.