# NSFileManager class reference

mircea@jupiter.elcom.pub.ro
ovidiu@net-community.com
n.pero@mi.flashnet.it
rfm@gnu.org
34637
2012-01-26 12:59:58 -0700 (Thu, 26 Jan 2012)
1997,1999-2005 Free Software Foundation, Inc.
File management
Path handling

The rules for path handling depend on the value in the `GNUSTEP_PATH_HANDLING` environment variable and, to some extent, on the platform on which the program is running.
The understood values of GNUSTEP_PATH_HANDLING are unix and windows. If GNUSTEP_PATH_HANDLING is any other value (or has not been set), GNUstep interprets this as meaning it should try to do-the-right-thing
In the default mode of operation the system is very tolerant of paths and allows you to work with both unix and windows style paths. The consequences of this are apparent in the path handling methods of NSString rather than in NSFileManager .

unix

On all Unix platforms, Path components are separated by slashes and file names may contain any character other than slash.
The file names. and.. are special cases meaning current directory and the parent of the current directory respectively.
Multiple adjacent slash characters are treated as a single separator.

Here are various examples:
/
An absolute path to the root directory.
/etc/motd
An absolute path to the file named motd in the subdirectory etc of the root directory.
..
A relative path to the parent of the current directory.
program.m
A relative path to the file program.m in the current directory.
Source/program.m
A relative path to the file program.m in the subdirectory Source of the current directory.
../GNUmakefile
A relative path to the file GNUmakefile in the directory above the current directory.
windows

On Microsoft Windows the native paths may be either UNC or drive-relative, so GNUstep supports both.
Either or both slash (/) and backslash (\) may be used as separators for path components in either type of name.
UNC paths follow the general form //host/share/path/file, but must at least contain the host and share parts, i.e. //host/share is a UNC path, but //host is not
Drive-relative names consist of an optional drive specifier (consisting of a single letter followed by a single colon) followed by an absolute or relative path.
In both forms, the names. and.. are refer to the current directory and the parent directory as in unix paths.

Here are various examples:
//host/share/file
An absolute UNC path to a file called file in the top directory of the export point share on host.
C:
A relative path to the current directory on drive C.
C:program.m
A relative path to the file program.m on drive C.
C:\program.m
An absolute path to the file program.m in the top level directory on drive C.
/Source\program.m
A drive-relative path to program.m in the directory Source on the current drive.
\\name
A drive-relative path to name in the top level directory on the current drive. The '\\' is treated as a single backslash as this is not a UNC name (there must be both a host and a share part in a UNC name).
gnustep

In the default mode, GNUstep handles both unix and windows paths so it treats both slash (/) and backslash (\) as separators and understands the windows UNC and drive relative path roots.
However, it treats any path beginning with a slash (/) as an absolute path if running on a unix system.

Portability

Attempting to pass absolute paths between applications working on different systems is fraught with difficulty... just don't do it.
Where paths need to be passed around (eg. in property lists or archives) you should pass relative paths and use a standard mechanism to construct an absolute path in the receiving application, for instance, appending the relative path to the home directory of a user.

Even using relative paths you should take care... Use only the slash (/) as a path separator, not backslash (\). Never use a backslash (\) in a file name. Avoid colons in file names. Use no more than three letters in a path extension. Remember that, while GNUstep will manipulate both windows and unix paths, any path actually used to reference a file or directory must be valid on the local system.
Tilde substitution

GNUstep handles substitution of tilde (~) as follows:
If a path is just ~ or begins ~/ then the value returned by NSHomeDirectory() is substituted for the tilde.
If a path is of the form ~name or begins with a string like ~name/ then name is used as the argument to NSHomeDirectoryForUser() and the return value from that method (if non-nil) is used to replace the tilde.

Software documentation for the NSDirectoryEnumerator class
Foundation/NSFileManager.h

This is a subclass of `NSEnumerator` which provides a full listing of all the files beneath a directory and its subdirectories. Instances can be obtained through [NSFileManager -enumeratorAtPath:].

This implementation is optimized and performance should be comparable to the speed of standard Unix tools for large directories.

The order in which directory contents are enumerated is undefined, and in the current implementation the natural order of the underlying filesystem is used.

directoryAttributes
Returns a dictionary containing the attributes of the directory at which enumeration started.
The contents of this dictionary are as produced by [NSFileManager -fileAttributesAtPath:traverseLink:]
fileAttributes
Returns a dictionary containing the attributes of the file currently being enumerated.
The contents of this dictionary are as produced by [NSFileManager -fileAttributesAtPath:traverseLink:]
skipDescendents
Informs the receiver that any descendents of the current directory should be skipped rather than enumerated. Use this to avoid enumerating the contents of directories you are not interested in.
Software documentation for the NSFileManager class
Foundation/NSFileManager.h
This is the main class for platform-independent management of the local filesystem, which allows you to read and save files, create/list directories, and move or delete files and directories. In addition to simply listing directories, you may obtain an NSDirectoryEnumerator instance for recursive directory contents enumeration.
defaultManager
Returns a shared default file manager which may be used throughout an application.
attributesOfItemAtPath:
path
error:
error
If a file (or directory etc) exists at the specified path, and can be queried for its attributes, this method returns a dictionary containing the various attributes of that file. Otherwise `nil` is returned.
If an error occurs, error describes the problem. Pass NULL if you do not want error information.

The dictionary keys for attributes are -

`NSFileAppendOnly`
NSNumber... boolean
`NSFileCreationDate`
NSDate when the file was created (if supported)
`NSFileDeviceIdentifier`
NSNumber (identifies the device on which the file is stored)
`NSFileExtensionHidden`
NSNumber... boolean
`NSFileGroupOwnerAccountName`
NSString name of the file group
`NSFileGroupOwnerAccountID`
NSNumber ID of the file group
`NSFileHFSCreatorCode`
NSNumber not used
`NSFileHFSTypeCode`
NSNumber not used
`NSFileImmutable`
NSNumber... boolean
`NSFileModificationDate`
NSDate when the file was last modified
`NSFileOwnerAccountName`
NSString name of the file owner
`NSFileOwnerAccountID`
NSNumber ID of the file owner
`NSFilePosixPermissions`
NSNumber posix access permissions mask
`NSFileReferenceCount`
NSNumber number of links to this file
`NSFileSize`
NSNumber size of the file in bytes
`NSFileSystemFileNumber`
NSNumber the identifier for the file on the filesystem
`NSFileSystemNumber`
NSNumber the filesystem on which the file is stored
`NSFileType`
NSString the type of file

The NSDictionary class also has a set of convenience accessor methods which enable you to get at file attribute information more efficiently than using the keys above to extract it. You should generally use the accessor methods where they are available.

[NSDictionary(NSFileAttributes) -fileCreationDate]
[NSDictionary(NSFileAttributes) -fileExtensionHidden]
[NSDictionary(NSFileAttributes) -fileHFSCreatorCode]
[NSDictionary(NSFileAttributes) -fileHFSTypeCode]
[NSDictionary(NSFileAttributes) -fileIsAppendOnly]
[NSDictionary(NSFileAttributes) -fileIsImmutable]
[NSDictionary(NSFileAttributes) -fileSize]
[NSDictionary(NSFileAttributes) -fileType]
[NSDictionary(NSFileAttributes) -fileOwnerAccountName]
[NSDictionary(NSFileAttributes) -fileOwnerAccountID]
[NSDictionary(NSFileAttributes) -fileGroupOwnerAccountName]
[NSDictionary(NSFileAttributes) -fileGroupOwnerAccountID]
[NSDictionary(NSFileAttributes) -fileModificationDate]
[NSDictionary(NSFileAttributes) -filePosixPermissions]
[NSDictionary(NSFileAttributes) -fileSystemNumber]
[NSDictionary(NSFileAttributes) -fileSystemFileNumber]
changeCurrentDirectoryPath:
path
Changes the current directory used for all subsequent operations.
All non-absolute paths are interpreted relative to this directory.
The current directory is set on a per-task basis, so the current directory for other file manager instances will also be changed by this method.
changeFileAttributes:
attributes
atPath:
path
Change the attributes of the file at path to those specified.
Returns `YES` if all requested changes were made (or if the dictionary was `nil` or empty, so no changes were requested), `NO` otherwise.
On failure, some of the requested changes may have taken place.
componentsToDisplayForPath:
path
Returns an array of path components suitably modified for display to the end user. This modification may render the returned strings unusable for path manipulation, so you should work with two arrays... one returned by this method (for display to the user), and a parallel one returned by [NSString -pathComponents] (for path manipulation).
contentsAtPath:
path
Reads the file at path an returns its contents as an NSData object.
If an error occurs or if path specifies a directory etc then `nil` is returned.
contentsEqualAtPath:
path1
andPath:
path2
Returns `YES` if the contents of the file or directory at path1 are the same as those at path2.
If path1 and path2 are files, this is a simple comparison. If they are directories, the contents of the files in those subdirectories are compared recursively.
Symbolic links are not followed.
A comparison checks first file identity, then size, then content.
contentsOfDirectoryAtPath:
path
error:
error
Returns an array of the contents of the specified directory.
The listing does not recursively list subdirectories.
The special files '.' and '..' are not listed.
Indicates an error by returning `nil` (eg. if path is not a directory or it can't be read for some reason).
copyItemAtPath:
src
toPath:
dst
error:
error
Description forthcoming.
copyPath:
source
toPath:
destination
handler:
handler
Copies the file or directory at source to destination, using a handler object which should respond to [NSObject(NSFileManagerHandler) -fileManager:willProcessPath:] and [NSObject(NSFileManagerHandler) -fileManager:shouldProceedAfterError:] messages.
Will not copy to a destination which already exists.
createDirectoryAtPath:
path
attributes:
attributes
Creates a new directory, and sets its attributes as specified.
Creates other directories in the path as necessary.
Returns `YES` on success, `NO` on failure.
createDirectoryAtPath:
path
withIntermediateDirectories:
flag
attributes:
attributes
error:
error
Creates a new directory and all intermediate directories if flag is `YES`, creates only the last directory in the path if flag is `NO`. The directory is created with the attributes specified in attributes and any error is returned in error.
returns `YES` on success, `NO` on failure.
createFileAtPath:
path
contents:
contents
attributes:
attributes
Creates a new file, and sets its attributes as specified.
Initialises the file content with the specified data.
Returns `YES` on success, `NO` on failure.
createSymbolicLinkAtPath:
path
pathContent:
otherPath
Creates a symbolic link at path which links to the location specified by otherPath.
currentDirectoryPath
Returns the current working directory used by all instance of the file manager in the current task.
directoryContentsAtPath:
path
Returns an array of the contents of the specified directory.
The listing does not recursively list subdirectories.
The special files '.' and '..' are not listed.
Indicates an error by returning `nil` (eg. if path is not a directory or it can't be read for some reason).
displayNameAtPath:
path
Returns the name of the file or directory at path. Converts it into a format for display to an end user. This may render it unusable as part of a file/path name.
For instance, if a user has elected not to see file extensions, this method may return filenames with the extension removed.
The default operation is to return the result of calling [NSString -lastPathComponent] on the path.
enumeratorAtPath:
path

Returns an enumerator which can be used to return each item with the directory at path in turn.

The enumeration is recursive... following all nested subdirectories.

The order in which directory contents are enumerated is undefined, and in the current implementation the natural order of the underlying filesystem is used.

fileAttributesAtPath:
path
traverseLink:
flag
If a file (or directory etc) exists at the specified path, and can be queried for its attributes, this method returns a dictionary containing the various attributes of that file. Otherwise `nil` is returned.
If the flag is `NO` and the file is a symbolic link, the attributes of the link itself (rather than the file it points to) are returned.

The dictionary keys for attributes are -

`NSFileAppendOnly`
NSNumber... boolean
`NSFileCreationDate`
NSDate when the file was created (if supported)
`NSFileDeviceIdentifier`
NSNumber (identifies the device on which the file is stored)
`NSFileExtensionHidden`
NSNumber... boolean
`NSFileGroupOwnerAccountName`
NSString name of the file group
`NSFileGroupOwnerAccountID`
NSNumber ID of the file group
`NSFileHFSCreatorCode`
NSNumber not used
`NSFileHFSTypeCode`
NSNumber not used
`NSFileImmutable`
NSNumber... boolean
`NSFileModificationDate`
NSDate when the file was last modified
`NSFileOwnerAccountName`
NSString name of the file owner
`NSFileOwnerAccountID`
NSNumber ID of the file owner
`NSFilePosixPermissions`
NSNumber posix access permissions mask
`NSFileReferenceCount`
NSNumber number of links to this file
`NSFileSize`
NSNumber size of the file in bytes
`NSFileSystemFileNumber`
NSNumber the identifier for the file on the filesystem
`NSFileSystemNumber`
NSNumber the filesystem on which the file is stored
`NSFileType`
NSString the type of file

The NSDictionary class also has a set of convenience accessor methods which enable you to get at file attribute information more efficiently than using the keys above to extract it. You should generally use the accessor methods where they are available.

[NSDictionary(NSFileAttributes) -fileCreationDate]
[NSDictionary(NSFileAttributes) -fileExtensionHidden]
[NSDictionary(NSFileAttributes) -fileHFSCreatorCode]
[NSDictionary(NSFileAttributes) -fileHFSTypeCode]
[NSDictionary(NSFileAttributes) -fileIsAppendOnly]
[NSDictionary(NSFileAttributes) -fileIsImmutable]
[NSDictionary(NSFileAttributes) -fileSize]
[NSDictionary(NSFileAttributes) -fileType]
[NSDictionary(NSFileAttributes) -fileOwnerAccountName]
[NSDictionary(NSFileAttributes) -fileOwnerAccountID]
[NSDictionary(NSFileAttributes) -fileGroupOwnerAccountName]
[NSDictionary(NSFileAttributes) -fileGroupOwnerAccountID]
[NSDictionary(NSFileAttributes) -fileModificationDate]
[NSDictionary(NSFileAttributes) -filePosixPermissions]
[NSDictionary(NSFileAttributes) -fileSystemNumber]
[NSDictionary(NSFileAttributes) -fileSystemFileNumber]
fileExistsAtPath:
path
Returns `YES` if a file (or directory etc) exists at the specified path.
fileExistsAtPath:
path
isDirectory:
isDirectory
Returns `YES` if a file (or directory etc) exists at the specified path.
If the isDirectory argument is not a nul pointer, stores a flag in the location it points to, indicating whether the file is a directory or not.
fileSystemAttributesAtPath:
path
Returns a dictionary containing the filesystem attributes for the specified path (or `nil` if the path is not valid).
`NSFileSystemSize`
NSNumber the size of the filesystem in bytes
`NSFileSystemFreeSize`
NSNumber the amount of unused space on the filesystem in bytes
`NSFileSystemNodes`
NSNumber the number of nodes in use to store files
`NSFileSystemFreeNodes`
NSNumber the number of nodes available to create files
`NSFileSystemNumber`
NSNumber the identifying number for the filesystem
fileSystemRepresentationWithPath:
path
Convert from OpenStep internal string format to a string in the local filesystem format, suitable for passing to system functions.
This representation may vary between filesystems.
Converts the standard path separator ('/') and path extension ('.') characters to the local representation if necessary.
On mingw32 systems, the filesystem representation is 16-bit unicode and is expected to be used in conjunction with the variants of system calls which work with unicode strings.
Raises an exception if the character conversion is not possible.
isDeletableFileAtPath:
path
Returns `YES` if a file (or directory etc) exists at the specified path and is deletable.
isExecutableFileAtPath:
path
Returns `YES` if a file (or directory etc) exists at the specified path and is executable (if a directory is executable, you can access its contents).
isReadableFileAtPath:
path
Returns `YES` if a file (or directory etc) exists at the specified path and is readable.
isWritableFileAtPath:
path
Returns `YES` if a file (or directory etc) exists at the specified path and is writable.
linkPath:
source
toPath:
destination
handler:
handler

Links the file or directory at source to destination, using a handler object which should respond to [NSObject(NSFileManagerHandler) -fileManager:willProcessPath:] and [NSObject(NSFileManagerHandler) -fileManager:shouldProceedAfterError:] messages.

If the destination is a directory, the source path is linked into that directory, otherwise the destination must not exist, but its parent directory must exist and the source will be linked into the parent as the name specified by the destination.

If the source is a symbolic link, it is copied to the destination.
If the source is a directory, it is copied to the destination and its contents are linked into the new directory.
Otherwise, a hard link is made from the destination to the source.

moveItemAtPath:
src
toPath:
dst
error:
error
Description forthcoming.
movePath:
source
toPath:
destination
handler:
handler
Moves the file or directory at source to destination, using a handler object which should respond to [NSObject(NSFileManagerHandler) -fileManager:willProcessPath:] and [NSObject(NSFileManagerHandler) -fileManager:shouldProceedAfterError:] messages. Will not move to a destination which already exists.
pathContentOfSymbolicLinkAtPath:
path
Returns the name of the file or directory that the symbolic link at path points to.
removeFileAtPath:
path
handler:
handler
Removes the file or directory at path, using a handler object which should respond to [NSObject(NSFileManagerHandler) -fileManager:willProcessPath:] and [NSObject(NSFileManagerHandler) -fileManager:shouldProceedAfterError:] messages.
removeItemAtPath:
path
error:
error
Description forthcoming.
stringWithFileSystemRepresentation:
string
length:
len
Convert to OpenStep internal string format from a string in the local filesystem format, as returned by system functions.
This representation may vary between filesystems.
The GNUstep version of this method currently does not bother to change any path separator and extension characters to the standard values ('/' and '.' respectively) as the path handling methods of NSString should be able to handle native format strings.
On mingw32 systems, the filesystem representation is 16-bit unicode and is expected to have come from the variant of a system call which works with unicode strings.
subpathsAtPath:
path
Returns an array containing the (relative) paths of all the items in the directory at path.
The listing follows all subdirectories, so it can produce a very large array... use with care.
Software documentation for the NSDictionary(NSFileAttributes) category
Foundation/NSFileManager.h
Convenience methods for accessing named file attributes in a dictionary.
fileCreationDate
Return the file creation date attribute (or `nil` if not found).
fileExtensionHidden
Return the file extension hidden attribute (or `NO` if not found).
fileGroupOwnerAccountID
Return an NSNumber with the numeric value of the NSFileGroupOwnerAccountID attribute in the dictionary, or `nil` if the attribute is not present.
fileGroupOwnerAccountName
Return the file group owner account name attribute or `nil` if not present.
fileHFSCreatorCode
Returns HFS creator attribute (OS X).
fileHFSTypeCode
Returns HFS type code attribute (OS X).
fileIsAppendOnly
Return the file append only attribute (or `NO` if not found).
fileIsImmutable
Return the file immutable attribute (or `NO` if not found).
fileModificationDate
Return the file modification date attribute (or `nil` if not found)
fileOwnerAccountID
Return an NSNumber with the numeric value of the NSFileOwnerAccountID attribute in the dictionary, or `nil` if the attribute is not present.
fileOwnerAccountName
Return the file owner account name attribute or `nil` if not present.
filePosixPermissions
Return the file posix permissions attribute (or NSNotFound if the attribute is not present in the dictionary).
fileSize
Return the size of the file, or NSNotFound if the file size attribute is not found in the dictionary.
fileSystemFileNumber
Return the file system file identification number attribute or NSNotFound if the attribute is not present in the dictionary).
fileSystemNumber
Return the file system number attribute (or NSNotFound if the attribute is not present in the dictionary).
fileType
Return the file type attribute or `nil` if not present.
Software documentation for the NSObject(NSFileManagerHandler) informal protocol
Foundation/NSFileManager.h
An informal protocol to which handler objects should conform if they wish to deal with copy and move operations performed by NSFileManager.
fileManager:
fileManager
shouldProceedAfterError:
errorDictionary

When an error occurs during a copy or move operation, the file manager will send this message to the handler, and will use the return value to determine whether the operation should proceed. If the method returns `YES` then the operation will proceed after the error, if it returns `NO` then it will be aborted.

If the handler does not implement this method it will be treated as if it returns `NO`.

The error dictionary contains the following "Error" contains a description of the error. "Path" contains the path that is being processed when an error occurred. If an error occurs during an operation involving two files, like copying, and it is not clear which file triggers the error it will default to the source file. "FromPath" (Optional) contains the path involved in reading. "ToPath" (Optional) contains the path involved in writing.

Note that the `FromPath` is a GNUstep extension.

Also the `FromPath` and `ToPath` are filled in when appropriate. So when copying a file they will typically both have a value and when reading only `FromPath`.

fileManager:
fileManager
willProcessPath:
path
The file manager sends this method to the handler immediately before performing part of a directory move or copy operation. This provides the handler object with information it can use in the event of an error, to decide whether processing should proceed after the error.