How can I determine whether a certain path points to a hidden file/folder?
NSString *file = @"/my/file/some.where";
BOOL fileIsHidden = // <-- what do I do here?
I know that hidden files are prefixed by a period. This is not the only criteria for a file to be hidden. I've read somewhere that there's a .hidden file that also configures what files are hidden.
Is there a Cocoa/Carbon way to find this out easily without rewriting all this logic and gathering information from various sources?
EDIT: the kLSItemInfoIsInvisible check seems to work for some files. It doesn't seem to hide:
/dev /etc /tmp /var
All these are hidden by Finder by default.
As the poster pointed out, it doesn't seem to work on /etc and /var and what not, so I modified the method.
It now takes a "isFile" boolean, YES means its a file, NO means a directory
BOOL isInvisible(NSString *str, BOOL isFile){
CFURLRef inURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)str, kCFURLPOSIXPathStyle, isFile);
LSItemInfoRecord itemInfo;
LSCopyItemInfoForURL(inURL, kLSRequestAllFlags, &itemInfo);
BOOL isInvisible = itemInfo.flags & kLSItemInfoIsInvisible;
return (isInvisible != 0);
}
int main(){
NSLog(@"%d",isInvisible(@"/etc",NO)); // => 1
NSLog(@"%d",isInvisible(@"/Users",NO)); // => 0
NSLog(@"%d",isInvisible(@"/mach_kernel",YES)); // => 1
}
It seems to work on everything now!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With