SBTFileOperation.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // SBTFileOperation.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/8/14.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class SBTFileOperation: NSObject {
  10. // 复制一个文件,到目标位置
  11. static func copyFile(sourceUrl:String, targetUrl:String) {
  12. let fileManager = FileManager.default
  13. do{
  14. try fileManager.copyItem(atPath: sourceUrl, toPath: targetUrl)
  15. print("Success to copy file.--成功拷贝")
  16. g_showHUD("savesuccess".da_localizedStr())
  17. }catch{
  18. print("Failed to copy file. ---拷贝失败")
  19. g_showHUD("savefail".da_localizedStr())
  20. }
  21. }
  22. // 移动文件到目标位置
  23. static func movingFile(sourceUrl:String, targetUrl:String){
  24. let fileManager = FileManager.default
  25. let targetUrl = targetUrl
  26. print("targetUrl = \(targetUrl)")
  27. do{
  28. try fileManager.moveItem(atPath: sourceUrl, toPath: targetUrl)
  29. print("Succsee to move file.")
  30. }catch{
  31. print("Failed to move file.")
  32. }
  33. }
  34. // 删除目标文件
  35. static func removeFile(sourceUrl:String){
  36. let fileManger = FileManager.default
  37. do{
  38. try fileManger.removeItem(atPath: sourceUrl)
  39. print("Success to remove file.")
  40. }catch{
  41. print("Failed to remove file.")
  42. }
  43. }
  44. // 删除目标文件夹下所有的内容
  45. static func removeFolder(folderUrl:String){
  46. let fileManger = FileManager.default
  47. // 然后获得所有该目录下的子文件夹
  48. let files:[AnyObject]? = fileManger.subpaths(atPath: folderUrl)! as [AnyObject]
  49. // 创建一个循环语句,用来遍历所有子目录
  50. for file in files!
  51. {
  52. do{
  53. //删除指定位置的内容
  54. try fileManger.removeItem(atPath: folderUrl + "/\(file)")
  55. print("Success to remove folder.")
  56. }catch{
  57. print("Failder to remove folder")
  58. }
  59. }
  60. }
  61. // 遍历目标文件夹
  62. static func listFolder(folderUrl:String){
  63. let manger = FileManager.default
  64. // 获得文档目录下所有的内容,以及子文件夹下的内容,在控制台打印所有的数组内容
  65. let contents = manger.enumerator(atPath: folderUrl)
  66. print("contents:\(String(describing: contents?.allObjects))")
  67. }
  68. }