Swift delete all files from particular Document Directory Location

Jam Ku

My Scenario, I am trying to delete all files from particular document directory by using document folder path. Here, every time I am saving file within my application document directory folder, by using below code I can’t able to delete files

let urlString: String = myurl.absoluteString
print("FILEURL:\(urlString)")

 do {
        try fm.removeItem(atPath: "\(myurl)")
    } catch let error as NSError {
        print(error.debugDescription)
    }
vadian

You are mixing up URL and String path

Either use the String related API

try fm.removeItem(atPath: myurl.path) // NEVER use .absoluteString for a file system path

or use the URL related API (recommended)

try fm.removeItem(at: myurl)

To remove all files get the file URLs in the enclosing directory with contentsOfDirectory(at:includingPropertiesForKeys:options:) and remove one by one

let fileManager = FileManager.default
do {
    let documentDirectoryURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let fileURLs = try fileManager.contentsOfDirectory(at: documentDirectoryURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
    for url in fileURLs {
       try fileManager.removeItem(at: url)
    }
} catch {
    print(error)
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to delete all files and directory except one named directory from a specific folder in centos

分類Dev

Delete all files with executable permission in a directory

分類Dev

how to extract the properties of all files and folders of a particular directory using vbscript?

分類Dev

Reading all files from a directory

分類Dev

How to recursively delete all .DS_STORE files from a directory on a Windows Machine?

分類Dev

Shell Script for Delete all files in directory and print count of deleted files

分類Dev

What is the command to remove files in Linux from a particular directory which are owned by a particular user?

分類Dev

Add all bib files from a directory to bookdown

分類Dev

C++ Delete all files and subfolders but keep the directory itself

分類Dev

Delete All Files Older Than Specified Time Within A Directory (Ubuntu)

分類Dev

How to move all files from current directory to upper directory?

分類Dev

Remove all files from within documentDirectory in Swift

分類Dev

How to list packages with files in a particular directory?

分類Dev

Zip all files in directory?

分類Dev

How to delete all unprotected rows in a range of particular sheets from Google Sheets using Apps Scripts

分類Dev

$(document).click from two location

分類Dev

How do I remove all files from wtihin a certain directory except for a child directory of that directory?

分類Dev

How to get all files from a directory in Azure BLOB

分類Dev

Select all files from directory that contain file with given name

分類Dev

How to clear data from all files present in a directory?

分類Dev

Can I delete all migration files and start from scratch?

分類Dev

Delete all characters after a certain character from a string in Swift

分類Dev

Delete all directories and its files older than a 60 days regardless of whether the directory is empty or not

分類Dev

Delete all files in a directory whose name do not match a line in a file list

分類Dev

How to delete particular no of elements from array in mongodb

分類Dev

Apply command to all files in a directory

分類Dev

git: Delete all tracked files?

分類Dev

How use minimum number of commands to copy all .txt files from all subdirectories to one directory?

分類Dev

Delete a Document with all Subcollections and Nested Subcollections in Firestore

Related 関連記事

  1. 1

    How to delete all files and directory except one named directory from a specific folder in centos

  2. 2

    Delete all files with executable permission in a directory

  3. 3

    how to extract the properties of all files and folders of a particular directory using vbscript?

  4. 4

    Reading all files from a directory

  5. 5

    How to recursively delete all .DS_STORE files from a directory on a Windows Machine?

  6. 6

    Shell Script for Delete all files in directory and print count of deleted files

  7. 7

    What is the command to remove files in Linux from a particular directory which are owned by a particular user?

  8. 8

    Add all bib files from a directory to bookdown

  9. 9

    C++ Delete all files and subfolders but keep the directory itself

  10. 10

    Delete All Files Older Than Specified Time Within A Directory (Ubuntu)

  11. 11

    How to move all files from current directory to upper directory?

  12. 12

    Remove all files from within documentDirectory in Swift

  13. 13

    How to list packages with files in a particular directory?

  14. 14

    Zip all files in directory?

  15. 15

    How to delete all unprotected rows in a range of particular sheets from Google Sheets using Apps Scripts

  16. 16

    $(document).click from two location

  17. 17

    How do I remove all files from wtihin a certain directory except for a child directory of that directory?

  18. 18

    How to get all files from a directory in Azure BLOB

  19. 19

    Select all files from directory that contain file with given name

  20. 20

    How to clear data from all files present in a directory?

  21. 21

    Can I delete all migration files and start from scratch?

  22. 22

    Delete all characters after a certain character from a string in Swift

  23. 23

    Delete all directories and its files older than a 60 days regardless of whether the directory is empty or not

  24. 24

    Delete all files in a directory whose name do not match a line in a file list

  25. 25

    How to delete particular no of elements from array in mongodb

  26. 26

    Apply command to all files in a directory

  27. 27

    git: Delete all tracked files?

  28. 28

    How use minimum number of commands to copy all .txt files from all subdirectories to one directory?

  29. 29

    Delete a Document with all Subcollections and Nested Subcollections in Firestore

ホットタグ

アーカイブ