Home > Mobile >  Check if a file or directory is hidden
Check if a file or directory is hidden

Time:11-27

On Mac, the first case, if the name of a file or dir is start with the ".", then it is hidden.

another case, for example:

/Users/USER_NAME/Library
/bin

Their names do not start with ".", but their is hidden.

I got all attributes of '/Users/USER_NAME/Library':

objective-c

NSError * error;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];

output:

{
    NSFileCreationDate = "2019-06-04 23:44:56  0000";
    NSFileExtensionHidden = 0;
    NSFileGroupOwnerAccountID = 20;
    NSFileGroupOwnerAccountName = staff;
    NSFileModificationDate = "2021-10-31 18:47:57  0000";
    NSFileOwnerAccountID = 501;
    NSFileOwnerAccountName = xxxxxxxx;
    NSFilePosixPermissions = 448;
    NSFileProtectionKey = NSFileProtectionCompleteUntilFirstUserAuthentication;
    NSFileReferenceCount = 79;
    NSFileSize = 2528;
    NSFileSystemFileNumber = 360719;
    NSFileSystemNumber = 16777221;
    NSFileType = NSFileTypeDirectory;
}

I not found whether to hide related attribute(s).

CodePudding user response:

You can check the fileURL resource key isHiddenKey if the fileURL is hidden or not. Note that if the file name begins with a period changing the isHidden value won't have any effect:

From the docs

If the resource is hidden because its name begins with a period, setting this value has no effect.


extension URL {
    var isHidden: Bool {
        get { (try? resourceValues(forKeys: [.isHiddenKey]))?.isHidden == true }
        set {
            var resourceValues = URLResourceValues()
            resourceValues.isHidden = newValue
            do {
                try setResourceValues(resourceValues)
            } catch {
                print("isHidden error:", error)
            }
        }
    }
}
  • Related