2
votes

Using Swift 3, I'm writing a video file to the documents folder. here's the path of the file.

file:///var/mobile/Containers/Data/Application/D031AB20-CB7E-4B14-9483-E7C3F4C0EC55/Documents/output11:10:04.mov

now I want to delete the file again using the filemanager. I retrieve the path above from the avassetwriter and use the outputurl I defined earlier to remove the file using the filemanager.

let fileManager = FileManager.default

   do {
       try fileManager.removeItem(at: (self.assetWriter?.outputURL)!)
      } catch let err {
        print(err)
      }

but I get the error that there is no such file or directory. using self.assetWriter?.outputURL.pathgives me the same error.

Error Domain=NSCocoaErrorDomain Code=4 "“output11/10/04.mov” couldn’t be removed." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/D031AB20-CB7E-4B14-9483-E7C3F4C0EC55/Documents/output11:10:04.mov, NSUserStringVariant=( Remove ), NSUnderlyingError=0x174240150 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

3
before deleting file , please check if file is exist at that path or not? As your error is showing that this file is not exists at location.Saurabh Prajapati
output11:10:04.mov is file name not t Error Domain=NSCocoaErrorDomain Code=4 " output11/10/04.movPrashant Tukadiya

3 Answers

4
votes

Do not use colons in file names.

(For legacy reasons) the OS treats them as path separators and replaces them with slashes.

3
votes

First Check weather the file exist at this path or not if exist then delete it :-

let file_manager = FileManager.init()
        if let url = self.assetWriter?.outputURL{
            if file_manager.fileExists(atPath: url.path){
                if file_manager.isDeletableFile(atPath: url.path){
                    do {
                        try file_manager.removeItem(at: url)
                    } catch let err {
                        print(err.localizedDescription)
                    }
                }
            }
        }
3
votes

remove "file://" string from starting.

 let path = documentPathValue.replacingOccurrences(of: "file://", with: "")

complete code

 func clearAllFilesFromDirectory() {

    let fileManager = FileManager.default
    let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
    let documentsPath = documentsUrl.path

    do {
        if let documentPathValue = documentsPath{

            let path = documentPathValue.replacingOccurrences(of: "file://", with: "")
            let fileNames = try fileManager.contentsOfDirectory(atPath: "\(path)")
            print("all files in cache: \(fileNames)")

            for fileName in fileNames {

                let tempPath = String(format: "%@/%@", path, fileName)

                //Check for specific file which you don't want to delete. For me .sqlite files
                if !tempPath.contains(".sql") {
                    try fileManager.removeItem(atPath: tempPath)
                } 
            }
        }

    } catch {
        print("Could not clear document directory \(error)")
    }
}