質問するログイン新規登録

質問編集履歴

2

一部修正

2021/09/22 02:59

投稿

tactaco
tactaco

スコア15

title CHANGED
File without changes
body CHANGED
@@ -50,52 +50,8 @@
50
50
 
51
51
 
52
52
  **
53
- **↓変更**
54
53
 
55
- func CopyTest() {
56
- let fileManager = FileManager.default
57
- let libraryUrl = fileManager.urls(for: .libraryDirectory, in: .userDomainMask)[0]
58
- let backupUrl = libraryUrl.appendingPathComponent("backUP/Documents",isDirectory: true)
59
- let documentUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
60
54
 
61
- //バックアップフォルダの中身を取得する
62
- if let itemUrls = try? fileManager.contentsOfDirectory(at: backupUrl, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])
63
- {
64
- //アイテムのURLを一つ一つ取得して'/Documents'の中へコピーする
65
- for itemUrl in itemUrls {
66
- let toUrl = documentUrl.appendingPathComponent(itemUrl.lastPathComponent)
67
- if (fileManager.fileExists(atPath: toUrl.path)) {
68
-
69
- //コピー先に同名のファイルがある場合にそれを消す為に定義
70
-  let maindocu4 = NSHomeDirectory() + "/Documents/" + "(toUrl)"
71
- let maindocUrl4 = URL(fileURLWithPath: maindocu4)
72
- let maindocfile4 = maindocUrl4.lastPathComponent
73
-  let maindocu5 = NSHomeDirectory() + "/Documents/" + "(maindocfile4)"
74
- let maindocuUrl5 = URL(fileURLWithPath: maindocu5)
75
- //同名のファイルがある場合の処理
76
- do {
77
- //一度消す
78
- try fileManager.removeItem(at: maindocuUrl5)
79
- //その後にコピー
80
- try fileManager.copyItem(at: itemUrl, to: toUrl)
81
- } catch {
82
- print("削除とコピーが失敗")
83
- }
84
- } else {
85
- do {
86
- try fileManager.copyItem(at: itemUrl, to: toUrl)
87
- } catch {
88
- print("コピーに失敗",error.localizedDescription)
89
- }
90
- }
91
- }
92
- }
93
- }
94
-
95
-
96
-
97
-
98
-
99
55
  **
100
56
  ```ここに言語名を入力
101
57
  swift

1

ソースコードを追加致しました

2021/09/22 02:59

投稿

tactaco
tactaco

スコア15

title CHANGED
File without changes
body CHANGED
@@ -48,6 +48,55 @@
48
48
  }
49
49
  }
50
50
 
51
+
52
+ **
53
+ **↓変更**
54
+
55
+ func CopyTest() {
56
+ let fileManager = FileManager.default
57
+ let libraryUrl = fileManager.urls(for: .libraryDirectory, in: .userDomainMask)[0]
58
+ let backupUrl = libraryUrl.appendingPathComponent("backUP/Documents",isDirectory: true)
59
+ let documentUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
60
+
61
+ //バックアップフォルダの中身を取得する
62
+ if let itemUrls = try? fileManager.contentsOfDirectory(at: backupUrl, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])
63
+ {
64
+ //アイテムのURLを一つ一つ取得して'/Documents'の中へコピーする
65
+ for itemUrl in itemUrls {
66
+ let toUrl = documentUrl.appendingPathComponent(itemUrl.lastPathComponent)
67
+ if (fileManager.fileExists(atPath: toUrl.path)) {
68
+
69
+ //コピー先に同名のファイルがある場合にそれを消す為に定義
70
+  let maindocu4 = NSHomeDirectory() + "/Documents/" + "(toUrl)"
71
+ let maindocUrl4 = URL(fileURLWithPath: maindocu4)
72
+ let maindocfile4 = maindocUrl4.lastPathComponent
73
+  let maindocu5 = NSHomeDirectory() + "/Documents/" + "(maindocfile4)"
74
+ let maindocuUrl5 = URL(fileURLWithPath: maindocu5)
75
+ //同名のファイルがある場合の処理
76
+ do {
77
+ //一度消す
78
+ try fileManager.removeItem(at: maindocuUrl5)
79
+ //その後にコピー
80
+ try fileManager.copyItem(at: itemUrl, to: toUrl)
81
+ } catch {
82
+ print("削除とコピーが失敗")
83
+ }
84
+ } else {
85
+ do {
86
+ try fileManager.copyItem(at: itemUrl, to: toUrl)
87
+ } catch {
88
+ print("コピーに失敗",error.localizedDescription)
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }
94
+
95
+
96
+
97
+
98
+
99
+ **
51
100
  ```ここに言語名を入力
52
101
  swift
53
102
  Xcode