티스토리 뷰

728x90
반응형

출발지에서 목적지로 파일이동

1. 출발지 폴더를 선택하고 정해진 하위 경로의 파일을 목적지로 이동

2. 목적지 폴더가 없을 경우 생성, 목적지에 동일한 파일명이 있을 경우 제거 (덮어쓰기)

 

동영상 서비스가 종료되어 해당 콘텐츠를 재생할 수 없습니다.

 

struct ContentView: View {
    
    @State private var sourcePath: URL?
    @State private var destinationPath: URL?
    @State private var message: String?
    
    var body: some View {
        VStack {
            Button("출발지 폴더 선택") {
                selectFolder { url in
                    sourcePath = url
                }
            }
            Text(sourcePath?.absoluteString ?? "")
            
            Button("목적지 폴더 선택") {
                selectFolder { url in
                    destinationPath = url
                }
            }
            Text(destinationPath?.absoluteString ?? "")
            
            Button("파일 이동") {
                moveFiles()
            }
            Text(message ?? "")
        }
    }
    
    /// 폴더 선택
    func selectFolder(completion: @escaping (URL) -> Void) {
        let openPanel = NSOpenPanel()
        openPanel.canChooseFiles = false
        openPanel.canChooseDirectories = true
        openPanel.begin { response in
            if response == .OK, let url = openPanel.urls.first {
                completion(url)
            }
        }
    }
    
    /// 파일 이동
    func moveFiles() {
        guard let sourcePath,
              let destinationPath else {
            message = "Source or destination folder not selected."
            return
        }
        
        let fileName = "img.png"
        let sourceURL = sourcePath.appendingPathComponent("A").appendingPathComponent(fileName)
        let destinationFolderURL = destinationPath.appendingPathComponent("B").appendingPathComponent("C")
        let destinationFileURL = destinationFolderURL.appendingPathComponent(fileName)
        
        let fileManager = FileManager.default
        
        if fileManager.fileExists(atPath: destinationFolderURL.path) { // 목적지 폴더가 있을 경우
            if fileManager.fileExists(atPath: destinationFileURL.path) { // img.png 파일이 있다면 삭제
                do {
                    try fileManager.removeItem(at: destinationFileURL)
                } catch {
                    message = "파일 삭제 실패: \(error.localizedDescription)"
                }
            }
        } else { // 목적지 폴더가 없을 경우
            do { // 폴더 생성
                try fileManager.createDirectory(at: destinationFolderURL, withIntermediateDirectories: true)
            } catch {
                message = "폴더 생성 실패: \(error.localizedDescription)"
            }
        }
        
        do { // 복사 시작
            try fileManager.copyItem(at: sourceURL, to: destinationFileURL)
            message = "복사 성공"
        } catch {
            message = "복사 실패: \(error.localizedDescription)"
        }
    }
}

 

지금은 1개의 파일만 예시로 작성했지만

회사에서 다수의 각기 다른 경로 파일들을 옮기는데 유용하게 사용중

해당 맥앱을 활용하여 일일히 파일 옮기는 시간 단축↓ , 업무 효율 향상↑

iOS

Swift

Xcode

728x90
반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/07   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함