Is the file hidden?

How to determine if a specific 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 have a period prefix. This is not the only criterion for hiding a file. I read somewhere that there is a .hidden file that also configures which files are hidden.

Is there a Cocoa / carbon way to easily find this without overwriting all this logic and gathering information from different sources?

EDIT: checking kLSItemInfoIsInvisible seems to work for some files. It does not seem to be hiding:

  / dev
 / etc
 / tmp
 / var

All of them are hidden by default Finder.

+4
source share
5 answers

As the poster pointed out, it does not seem to work on / etc and / var and what not, so I modified the method.

Now it takes a "isFile" boolean, YES means its file, NO means 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 } 

Everything seems to be working now!

+5
source

As far as I know, hidden files in OS X are defined either by a file name preceded by a period, or by a special β€œinvisible” bit that is tracked by Finder.

A few years ago, I had to write something that changed the visibility of this file, and I found that it was actually much more complicated than I expected. Its essence was to get the file Finder ( FInfo ) for the file and check for the presence of the kIsInvisible bit. Here's the method I wrote to switch file visibility - I think a lot of this is relevant to your task, although you obviously have to tweak it a bit.

 - (BOOL)toggleVisibilityForFile:(NSString *)filename isDirectory:(BOOL)isDirectory { // Convert the pathname to HFS+ FSRef fsRef; CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filename, kCFURLPOSIXPathStyle, isDirectory); if (!url) { NSLog(@"Error creating CFURL for %@.", filename); return NO; } if (!CFURLGetFSRef(url, &fsRef)) { NSLog(@"Error creating FSRef for %@.", filename); CFRelease(url); return NO; } CFRelease(url); // Get the file catalog info FSCatalogInfo *catalogInfo = (FSCatalogInfo *)malloc(sizeof(FSCatalogInfo)); OSErr err = FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, catalogInfo, NULL, NULL, NULL); if (err != noErr) { NSLog(@"Error getting catalog info for %@. The error returned was: %d", filename, err); free(catalogInfo); return NO; } // Extract the Finder info from the FSRef catalog info FInfo *info = (FInfo *)(&catalogInfo->finderInfo[0]); // Toggle the invisibility flag if (info->fdFlags & kIsInvisible) info->fdFlags &= ~kIsInvisible; else info->fdFlags |= kIsInvisible; // Update the file visibility err = FSSetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, catalogInfo); if (err != noErr) { NSLog(@"Error setting visibility bit for %@. The error returned was: %d", filename, err); free(catalogInfo); return NO; } free(catalogInfo); return YES; } 

Here's the Apple documentation for the Finder Interface if you would like more information. Hope this helps.

+4
source

From http://forums.macosxhints.com/archive/index.php/t-22641.html :

 BOOL isInvisibleCFURL(CFURLRef inURL) { LSItemInfoRecord itemInfo; LSCopyItemInfoForURL(inURL, kLSRequestAllFlags, &itemInfo); BOOL isInvisible = itemInfo.flags & kLSItemInfoIsInvisible; return isInvisible; } 

Update

Yeah! / Etc, / tmp and / var are invisible because they are actually symbolic links to / private / etc, / private / tmp and / private / var. If you tell Finder to be directly visited / closed (using the "Go to Folder" menu item), you will see that they are displayed just fine. (Thanks @IlDan for the tip)

I'm not sure the best way to handle this; this only matters if there is a visible symbolic link to the file inside the hidden folder. You may be able to simply manually exclude the symbolic links that are included in / private, but if now you may have to check the hidden status of each folder on the way up.

+3
source

I think the fact is that Finder is an interface to the user of the tree (s) of the file system. You want to ask Finder if he thinks the file is hidden or not, so you need an API to do this.

It seems that LSCopyItemInfoForURL does the job, as shown in other answers. This post is very helpful:

There are several ways for things to be considered invisible (on Mac OS X):

  • KLSItemInfoIsInvisible Finder flag set
  • file name starts with period
  • in the /.hidden file
  • invisible due to origin
  • invisible due to package

I do not copy everything, it is a long time, but well written.

+2
source

The philosophical bit first:

The file is not hidden. Finder stores its own internal data to determine if the file should be displayed in the directory list or not; and this information can be shared with other applications in the system.

However, if you do not use the file system browser, the corresponding definitions are usually transparently perceived (not intended for pun intended) by the internal NSOpenPanel and friends.

If you arbitrarily access a file and keep some semblance of ownership on the same file or you do not show the file (or not) in the user interface, it really does not matter if Finder considers it hidden or not.

As for the technical bit; since any application can (through the aforementioned and venerable NSOpenPanel ) access this information, it is probably available somewhere; but, as already noted, this requires a rather helpful analysis in CoreFoundation and LaunchServices.

The real issue is probably important to you.

+1
source

All Articles