Use atproto actions with ease in iOS shortcuts
at main 2.5 kB view raw
1// 2// DirectoryCleaner.swift 3// shortcut 4// 5// Created by Bailey Townsend on 7/17/25. 6// 7 8import Foundation 9 10class DirectoryCleaner { 11 12 // Get the total size of all files in a directory 13 static func getTotalSize(of directory: URL) -> Int64 { 14 let fileManager = FileManager.default 15 var totalSize: Int64 = 0 16 17 do { 18 let contents = try fileManager.contentsOfDirectory( 19 at: directory, includingPropertiesForKeys: [.fileSizeKey, .isDirectoryKey]) 20 21 for fileURL in contents { 22 let resourceValues = try fileURL.resourceValues(forKeys: [ 23 .fileSizeKey, .isDirectoryKey, 24 ]) 25 26 // Skip directories 27 if resourceValues.isDirectory == true { 28 continue 29 } 30 31 let fileSize = Int64(resourceValues.fileSize ?? 0) 32 totalSize += fileSize 33 } 34 } catch { 35 print("Error reading directory: \(error)") 36 } 37 38 return totalSize 39 } 40 41 // Clear all files in a directory 42 static func clearDirectory(_ directory: URL) throws { 43 let fileManager = FileManager.default 44 45 do { 46 let contents = try fileManager.contentsOfDirectory( 47 at: directory, includingPropertiesForKeys: [.isDirectoryKey]) 48 49 for fileURL in contents { 50 let resourceValues = try fileURL.resourceValues(forKeys: [.isDirectoryKey]) 51 52 // Only remove files, not subdirectories 53 if resourceValues.isDirectory != true { 54 try fileManager.removeItem(at: fileURL) 55 } 56 } 57 58 print("Directory cleared successfully") 59 } catch { 60 print("Error clearing directory: \(error)") 61 throw error 62 } 63 } 64 65 // Clear all files and subdirectories in a directory 66 static func clearDirectoryCompletely(_ directory: URL) throws { 67 let fileManager = FileManager.default 68 69 do { 70 let contents = try fileManager.contentsOfDirectory( 71 at: directory, includingPropertiesForKeys: nil) 72 73 for fileURL in contents { 74 try fileManager.removeItem(at: fileURL) 75 } 76 77 print("Directory cleared completely") 78 } catch { 79 print("Error clearing directory: \(error)") 80 throw error 81 } 82 } 83}