Changes

Jump to: navigation, search

Working with Directories in Objective-C

1,986 bytes added, 16:11, 10 November 2009
Getting the Attributes of a Directory
== Getting the Attributes of a Directory ==
 
The attributes of a directory can be obtained using the ''attributesOfItemAtPath'' method. This takes as arguments the path of the directory and . The results are returned in the form an ''NSDictionary'' dictionary object (for details of working with dictionary objects refer to [[Objective-C Dictionary Objects]]). The keys for this dictionary are as follows:
 
<tt>NSFileType</tt><br>
<tt>NSFileTypeDirectory</tt><br>
<tt>NSFileTypeRegular</tt><br>
<tt>NSFileTypeSymbolicLink</tt><br>
<tt>NSFileTypeSocket</tt><br>
<tt>NSFileTypeCharacterSpecial</tt><br>
<tt>NSFileTypeBlockSpecial</tt><br>
<tt>NSFileTypeUnknown</tt><br>
<tt>NSFileSize</tt><br>
<tt>NSFileModificationDate</tt><br>
<tt>NSFileReferenceCount</tt><br>
<tt>NSFileDeviceIdentifier</tt><br>
<tt>NSFileOwnerAccountName</tt><br>
<tt>NSFileGroupOwnerAccountName</tt><br>
<tt>NSFilePosixPermissions</tt><br>
<tt>NSFileSystemNumber</tt><br>
<tt>NSFileSystemFileNumber</tt><br>
<tt>NSFileExtensionHidden</tt><br>
<tt>NSFileHFSCreatorCode</tt><br>
<tt>NSFileHFSTypeCode</tt><br>
<tt>NSFileImmutable</tt><br>
<tt>NSFileAppendOnly</tt><br>
<tt>NSFileCreationDate</tt><br>
<tt>NSFileOwnerAccountID</tt><br>
<tt>NSFileGroupOwnerAccountID</tt><br>
 
 
For example, we can extract the creation date, file type and POSIX permissions for the ''/tmp'' directory using the following code excerpt:
 
<pre>
NSFileManager *filemgr;
NSDictionary *attribs;
 
filemgr = [NSFileManager defaultManager];
 
attribs = [filemgr attributesOfItemAtPath: @"/tmp" error: NULL];
 
NSLog (@"Created on %@", [attribs objectForKey: NSFileCreationDate]);
NSLog (@"File type %@", [attribs objectForKey: NSFileType]);
NSLog (@"POSIX Permissions %@", [attribs objectForKey: NSFilePosixPermissions]);
</pre>
 
When exucted on a Mac OS X system, we can expect to see the following output (note that ''/tmp'' on a Mac OS X system is a symbolic link ''private/tmp''):
 
<pre>
Created on 2009-01-14 07:34:32 -0500
File type NSFileTypeSymbolicLink
POSIX Permissions 493
</pre>

Navigation menu