4

I am trying to create a function that I can delete multiple files in Document Directory with a given file Extension.

So far I have the function below but I can I complete it on older to delete the files founds?

static func searchFilesDocumentsFolder(Extension: String) {

        let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

        do {
            let directoryUrls = try  NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
            //print(directoryUrls)
            let Files = directoryUrls.filter{ $0.pathExtension == Extension }.map{ $0.lastPathComponent }
            print("\(Extension) FILES:\n" + Files.description)
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
SNos
  • 3,430
  • 5
  • 42
  • 92

2 Answers2

2
for file in Files {
    try NSFileManager.defaultManager().removeItemAtPath(file)
}
Christian
  • 4,596
  • 1
  • 26
  • 33
  • Thank you, this worked however, from the list one file cannot be removed --- "20-01-2016_142531.mov” couldn’t be removed. Any Idea why? – SNos Jan 22 '16 at 22:33
  • the method should throw an error in this case, that you can debug. It should tell you the reason, maybe the file is opened by another process. – Christian Jan 22 '16 at 22:37
  • the only error I can see is `"20-01-2016_142531.mov” couldn’t be removed.` – SNos Jan 22 '16 at 22:40
  • 1
    The problem there is that you have a list of the file names, not a list of the file paths. You can simply remove `.map{$0.lastPathComponent}` – Leo Dabus Jan 22 '16 at 22:45
  • the NSError (error in your code) that is thrown has an error code and a userInfo dictionary that should tell you more. If this does not get you further, you should post a new question with this problem and the details of your NSError – Christian Jan 22 '16 at 22:46
  • I think that the correct approach should not depend on the pathExtension to filter them by type. I would list all files URLs and filter it using fileURL UTI type. This way you can also list the files that don't have a pathExtension but still are images http://stackoverflow.com/questions/28570627/how-to-find-file-uti-for-file-withouth-pathextension-in-a-path-in-swift/34772517?s=1|0.0000#34772517 – Leo Dabus Jan 22 '16 at 22:55
  • @Christian the correct solution would be to map the filtered URLs extracting its paths. `let Files = directoryUrls.filter{ $0.pathExtension == Extension }.map{ $0.path! }` – Leo Dabus Jan 25 '16 at 18:19
0

For Swift 3 and Swift 4.0

 let fileManager : FileManager = FileManager.default
    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
    let paths : NSArray = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) as NSArray
    let documentsDirectory = paths.object(at: 0) as! NSString
    print(documentsDirectory)
    let contents : NSArray =  try! fileManager.contentsOfDirectory(atPath: documentsDirectory as String) as NSArray
    let enumerator : NSEnumerator = contents.objectEnumerator()
    while let element = enumerator.nextObject() as? String
    {
        let fileName = element as NSString
        if fileName.pathExtension == "m4a"
        {
            let pathOfFile = documentsDirectory.appendingPathComponent(fileName as String)
            try! fileManager.removeItem(atPath: pathOfFile)

        }
    }
Ankit Goyal
  • 3,019
  • 1
  • 21
  • 26