0

Because directories are files in macOS and Linux too, I try to determine what type of file it is.

This is the code I have so far:

import Foundation

let filePath = "/Users/Sample/test.ext"

if FileManager.default.isReadableFile(atPath: filePath) {
    /// File does exist at this location
}

However, this code does not say whether the file is a regular file or a directory. I thought about using fileType() to get the exact file type.

Can anyone please help me? I tried a bit but I don't get it working and I am new to Swift.

Thanks in advance.

Marlene
  • 9
  • 1
  • Hi to whoever wrote a comment that disappeared now. This is not a duplicate, I asked about how determine the file type. The question in the duplicate box is about if a file is a directory but there are other file types too (see Accessing File Type Attributes https://developer.apple.com/documentation/foundation/fileattributetype#1788787). – Marlene Jun 24 '23 at 18:08
  • Your whole question is talking about determining if the file is a directory or not. If that’s not your actual question then please edit your question to clarify what exactly you are looking for. – HangarRash Jun 24 '23 at 18:58
  • Title says “determining file type”, body “I try to determine what type of file it is” and “this code does not say whether the file is a regular file”. Don’t know what I should change? – Marlene Jun 24 '23 at 19:02
  • 1
    Your question has *"Because directories are files in macOS and Linux too"* and more importantly *"However, this code does not say whether the file is a regular file or a directory."*. So it reads as if the file type you are asking about is whether it's a directory or a regular file. Update your question to include examples of different file types you want to know about if it's more than "directory" or "regular file". BTW - I'm not the only one that thinks the duplicate applies at the moment. Once you clarify the question, it can be reopened. – HangarRash Jun 24 '23 at 19:20
  • As I said, I don't know what to clarify. Even if you take the nitty gritty and say "determine if normal file or directory" then the alleged duplicate will answer if the file is a directory but still not if a normal file. As I said, looking to see if it's a directory is still and will be ever the wrong way to see if it's a normal file or not. And it doesn't matter if anyone agrees with you or not, quantity is not quality. – Marlene Jun 24 '23 at 20:11
  • https://developer.apple.com/documentation/foundation/filemanager/1410452-attributesofitem, https://stackoverflow.com/questions/55258342/get-the-attributes-of-every-item-in-the-document-directory-ios-swift – Joakim Danielson Jun 25 '23 at 13:18

0 Answers0