2

I've been trying to get Attributes such as File Type, Creation Date, File Size of all the items in the Document Directory.

Here is the code that I've been using, but it returns me just "NSFileTypeDirectory"

 let filemgr = FileManager.default
        
        
     do
     {
        let attribs: NSDictionary? = try filemgr.attributesOfItem(
            atPath: documentDirectoryPath!) as NSDictionary
        if let fileattribs = attribs
     {
            let type = fileattribs["NSFileType"] as! String
            print("File type \(type)")
     }
     }
     catch
     {
            print(error)
     }

I think its returning the attributes of Document folder.

Nikhil Viradiya
  • 87
  • 2
  • 15
  • 1
    That's because you are passing the path of `Documents Directory` and not the path of _file inside documents directory_ . You need to pass the complete path of file inside the documents directory for which you want the attributes – Shubham Bakshi Mar 20 '19 at 10:24
  • 1
    Unrelated but `attributesOfItem(atPath` returns non-optional `[FileAttributeKey : Any]` **not** optional `NSDictionary` and the `error` variable is never used. – vadian Mar 20 '19 at 10:26

2 Answers2

3

try this

let fileManager = FileManager.default
let documentdirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
let filePath = documentdirectory?.appendingPathComponent("your file name").path

do {
  let fileAttribute = try fileManager.attributesOfItem(atPath: filePath!)
  let fileSize = fileAttribute[FileAttributeKey.size] as! Int64
  let fileType = fileAttribute[FileAttributeKey.type] as! String
  let filecreationDate = fileAttribute[FileAttributeKey.creationDate] as! Date

} catch let error {
  print(error.localizedDescription)
Karthikeyan Bose
  • 1,244
  • 3
  • 17
  • 26
1

Based on @KarthikeyanBose's code: To get the infos for all the files in the Documents directory do this:

let fileManager = FileManager.default

if let documentsURLs = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
    do {
        let fileNames = try fileManager.contentsOfDirectory(atPath: documentsURLs.path)

        for fileName in fileNames {
            let fileURL = documentsURLs.appendingPathComponent(fileName)

            let fileAttribute = try fileManager.attributesOfItem(atPath: fileURL.path)
            let fileSize = fileAttribute[FileAttributeKey.size] as! Int64
            let fileType = fileAttribute[FileAttributeKey.type] as! String
            let filecreationDate = fileAttribute[FileAttributeKey.creationDate] as! Date
            let fileExtension = fileURL.pathExtension;

            print("Name: \(fileName), Size: \(fileSize), Type: \(fileType), Date: \(filecreationDate), Extension: \(fileExtension)")
        }
    } catch {
        print("Error: \(error)")
    }
} //Handle this "else" error too, even though this really shouldn't happen

This code prints e.g.:

Name: Deadpool.png, Size: 39225, Type: NSFileTypeRegular, Date: 2019-05-27 11:03:03 +0000, Extension: png
Neph
  • 1,823
  • 2
  • 31
  • 69