質問編集履歴

2

一部修正

2021/09/22 02:59

投稿

tactaco
tactaco

スコア15

test CHANGED
File without changes
test CHANGED
@@ -102,94 +102,6 @@
102
102
 
103
103
  **
104
104
 
105
- **↓変更**
106
-
107
-
108
-
109
- func CopyTest() {
110
-
111
- let fileManager = FileManager.default
112
-
113
- let libraryUrl = fileManager.urls(for: .libraryDirectory, in: .userDomainMask)[0]
114
-
115
- let backupUrl = libraryUrl.appendingPathComponent("backUP/Documents",isDirectory: true)
116
-
117
- let documentUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
118
-
119
-
120
-
121
- //バックアップフォルダの中身を取得する
122
-
123
- if let itemUrls = try? fileManager.contentsOfDirectory(at: backupUrl, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])
124
-
125
- {
126
-
127
- //アイテムのURLを一つ一つ取得して'/Documents'の中へコピーする
128
-
129
- for itemUrl in itemUrls {
130
-
131
- let toUrl = documentUrl.appendingPathComponent(itemUrl.lastPathComponent)
132
-
133
- if (fileManager.fileExists(atPath: toUrl.path)) {
134
-
135
-
136
-
137
- //コピー先に同名のファイルがある場合にそれを消す為に定義
138
-
139
-  let maindocu4 = NSHomeDirectory() + "/Documents/" + "(toUrl)"
140
-
141
- let maindocUrl4 = URL(fileURLWithPath: maindocu4)
142
-
143
- let maindocfile4 = maindocUrl4.lastPathComponent
144
-
145
-  let maindocu5 = NSHomeDirectory() + "/Documents/" + "(maindocfile4)"
146
-
147
- let maindocuUrl5 = URL(fileURLWithPath: maindocu5)
148
-
149
- //同名のファイルがある場合の処理
150
-
151
- do {
152
-
153
- //一度消す
154
-
155
- try fileManager.removeItem(at: maindocuUrl5)
156
-
157
- //その後にコピー
158
-
159
- try fileManager.copyItem(at: itemUrl, to: toUrl)
160
-
161
- } catch {
162
-
163
- print("削除とコピーが失敗")
164
-
165
- }
166
-
167
- } else {
168
-
169
- do {
170
-
171
- try fileManager.copyItem(at: itemUrl, to: toUrl)
172
-
173
- } catch {
174
-
175
- print("コピーに失敗",error.localizedDescription)
176
-
177
- }
178
-
179
- }
180
-
181
- }
182
-
183
- }
184
-
185
- }
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
105
 
194
106
 
195
107
 

1

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

2021/09/22 02:59

投稿

tactaco
tactaco

スコア15

test CHANGED
File without changes
test CHANGED
@@ -98,6 +98,104 @@
98
98
 
99
99
 
100
100
 
101
+
102
+
103
+ **
104
+
105
+ **↓変更**
106
+
107
+
108
+
109
+ func CopyTest() {
110
+
111
+ let fileManager = FileManager.default
112
+
113
+ let libraryUrl = fileManager.urls(for: .libraryDirectory, in: .userDomainMask)[0]
114
+
115
+ let backupUrl = libraryUrl.appendingPathComponent("backUP/Documents",isDirectory: true)
116
+
117
+ let documentUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
118
+
119
+
120
+
121
+ //バックアップフォルダの中身を取得する
122
+
123
+ if let itemUrls = try? fileManager.contentsOfDirectory(at: backupUrl, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])
124
+
125
+ {
126
+
127
+ //アイテムのURLを一つ一つ取得して'/Documents'の中へコピーする
128
+
129
+ for itemUrl in itemUrls {
130
+
131
+ let toUrl = documentUrl.appendingPathComponent(itemUrl.lastPathComponent)
132
+
133
+ if (fileManager.fileExists(atPath: toUrl.path)) {
134
+
135
+
136
+
137
+ //コピー先に同名のファイルがある場合にそれを消す為に定義
138
+
139
+  let maindocu4 = NSHomeDirectory() + "/Documents/" + "(toUrl)"
140
+
141
+ let maindocUrl4 = URL(fileURLWithPath: maindocu4)
142
+
143
+ let maindocfile4 = maindocUrl4.lastPathComponent
144
+
145
+  let maindocu5 = NSHomeDirectory() + "/Documents/" + "(maindocfile4)"
146
+
147
+ let maindocuUrl5 = URL(fileURLWithPath: maindocu5)
148
+
149
+ //同名のファイルがある場合の処理
150
+
151
+ do {
152
+
153
+ //一度消す
154
+
155
+ try fileManager.removeItem(at: maindocuUrl5)
156
+
157
+ //その後にコピー
158
+
159
+ try fileManager.copyItem(at: itemUrl, to: toUrl)
160
+
161
+ } catch {
162
+
163
+ print("削除とコピーが失敗")
164
+
165
+ }
166
+
167
+ } else {
168
+
169
+ do {
170
+
171
+ try fileManager.copyItem(at: itemUrl, to: toUrl)
172
+
173
+ } catch {
174
+
175
+ print("コピーに失敗",error.localizedDescription)
176
+
177
+ }
178
+
179
+ }
180
+
181
+ }
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ **
198
+
101
199
  ```ここに言語名を入力
102
200
 
103
201
  swift