0

I need to have 2 methods : one for knowing if a file is hidden and the other one to set a file as hidden. I didn't get the aswer in developper documentation...

Anybody?

Thanks a lot ! Jérôme

  • It's in there. [Look just a](http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/ManagingFIlesandDirectories/ManagingFIlesandDirectories.html%23//apple_ref/doc/uid/TP40010672-CH6-SW12) little [bit harder](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html). – jscs Jul 27 '13 at 20:36

2 Answers2

3

Files that begin with a "." will be hidden in Finder by default, so you could test if the file start with a dot, for example :

NSString* filename = //Something
if([string hasPrefix:@"."]) {
  //The file is hidden
}

To make the file invisible you could rename the file prepending a period to the name.

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 1
    Sure but is there an attribute "hidden" for any file ? – user2600797 Jul 27 '13 at 20:50
  • 3
    Yes, [there is](http://stackoverflow.com/questions/15755579/creating-and-moving-an-invisible-file-with-os-x-10-8-terminal), @user2600797. – jscs Jul 27 '13 at 21:09
  • @user2600797 actually that's NOT a file attribute, but rather a shell/BSD common-line thing. in the list of file attributes I haven't yet found a "hidden" attribute. There is only that unix convention of not showing in the UI (or ls if you consider the Terminal some kind of UI) files whose name begins with dot. – Motti Shneor Jul 02 '20 at 10:57
3

You can use NSFileManager to list files in a directory.

NSURL *directoryURL = [NSURL fileURLWithPath:(NSString*)path];

NSArray *filteredContents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:directoryURL
         includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey]
                            options:NSDirectoryEnumerationSkipsHiddenFiles
                              error:nil];

With the NSDirectoryEnumerationSkipsHiddenFiles option specified, it will skip all hidden files in the directory. You can then perform a similar method, one that returns all files in a directory.

NSArray *allContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:(NSString *path) error:nil];

Whatever files are in allContents that aren't in filteredContents will presumably be hidden files.

eswick
  • 519
  • 4
  • 16
  • but that would not help write the required methods: one to tell you if a single file is hidden (or do you recommend this trick of comparing 2 iterations of parent directory, just to see if a specific file is hidden?) and how to MAKE a file hidden. – Motti Shneor Jul 02 '20 at 10:55