[swift] Swift에서 디렉토리 삭제 시 파일 복구하기

Swift에서 디렉토리를 삭제하는 경우, 삭제된 파일을 복구해야 하는 경우가 발생할 수 있습니다. 이때 파일을 복구하는 방법에 대해 알아봅시다.

파일 복구하기

import Foundation

func restoreFiles(directoryPath: String) {
    let fileManager = FileManager.default
    do {
        let files = try fileManager.contentsOfDirectory(atPath: directoryPath)
        for file in files {
            try fileManager.moveItem(atPath: "\(directoryPath)/\(file)", toPath: "newDirectoryPath/\(file)")
        }
    } catch {
        print("Error: \(error)")
    }
}

위의 코드는 FileManager를 사용하여 디렉토리 내의 파일을 복구하는 방법을 보여줍니다. restoreFiles 함수는 지정된 디렉토리에서 파일 목록을 가져와 새로운 디렉토리에 파일을 이동시킵니다.

이제 파일을 복구하기 위해 위의 코드를 적용하여 디렉토리에서 삭제된 파일을 복구할 수 있습니다.

위의 방법을 통해 Swift에서 디렉토리 내의 파일을 쉽게 복구할 수 있습니다.

결론

Swift에서 디렉토리를 삭제하는 경우, 파일을 복구하는 것은 중요한 작업일 수 있습니다. FileManager를 사용하여 삭제된 파일을 복구할 수 있으며, 파일을 옮기는 방법을 활용할 수 있습니다.