forked from bestiosdeveloper/Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManagerExtension.swift
executable file
·105 lines (92 loc) · 3.34 KB
/
FileManagerExtension.swift
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// FileManagerExtension.swift
//
// Created by Pramod Kumar on 19/09/17.
// Copyright © 2017 Pramod Kumar. All rights reserved.
//
import Foundation
import UIKit
extension FileManager{
///Prints Core Data URL
@nonobjc class var printCoreDataURL:Void{
printD(self.default.urls(for: .documentDirectory, in: .userDomainMask))
}
@nonobjc class var documentDirectoryUrl: URL {
return self.default.urls(for: .documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)[0]
}
///Clears all files of 'Document' directory
@nonobjc class var clearDocumentDirectory:Void{
do {
let directoryContents : [URL] = try self.default.contentsOfDirectory(at: documentDirectoryUrl, includingPropertiesForKeys: nil, options: self.DirectoryEnumerationOptions())
for videoUrl in directoryContents {
if URL(fileURLWithPath:videoUrl.path).pathExtension.isAnyUrl {
removeFile(atPath: videoUrl.path)
}
}
}
catch {
}
}
///Clears all files of 'Cache' directory
@nonobjc class var clearCacheDirectory:Void{
let documentsUrl = self.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
do {
let directoryContents : [URL] = try self.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: self.DirectoryEnumerationOptions())
for videoUrl in directoryContents {
if URL(fileURLWithPath:videoUrl.path).pathExtension.isAnyUrl {
removeFile(atPath: videoUrl.path)
}
}
} catch {
}
}
///Clears all files of 'Temp' directory
@nonobjc class var clearTempDirectory:Void{
do {
let tempDirectory = try self.default.contentsOfDirectory(atPath: NSTemporaryDirectory())
for file in tempDirectory {
try self.default.removeItem(atPath: "\(NSTemporaryDirectory())\(file)")
}
}
catch{
}
}
///Removes a file at a given path
class func removeFile(atPath filePath: String) {
let fileManager = self.default
if fileManager.fileExists(atPath: filePath) {
do {
try fileManager.removeItem(atPath: filePath)
}
catch{
}
}
}
///Removes all files at a given directroy path
class func removeFiles(atDirectroyPath directroyPath: String){
do{
let array = try self.default.contentsOfDirectory(atPath: directroyPath)
for file in array{
removeFile(atPath: "\(directroyPath)/\(file)")
}
}
catch {
}
}
class func removeFiles(atDirectroyPath directroyPath: String, except:[String]){
do{
let array = try self.default.contentsOfDirectory(atPath: directroyPath)
for file in array{
if !except.contains(file){
removeFile(atPath: "\(directroyPath)/\(file)")
}
}
}
catch {
}
}
class func isFileExists(_ filePath: String) -> Bool {
let fileManager = self.default
return fileManager.fileExists(atPath: filePath)
}
}