This article describes the NSFileManager
class and how you use it.
Overview
Broken Links
Path Utilities
NSFileManager
enables you to perform many generic file-system operations. With it you can:
Create directories and files.
Extract the contents of files (as NSData
objects).
Change your current working location in the file system.
Copy, move, and link files and directories.
Remove files, links, and directories.
Determine the attributes of a file, a directory, or the file system.
Set the attributes of a file or directory.
Make and evaluate symbolic links.
Determine the contents of directories.
Compare files and directories for equality.
Besides offering a useful range of generic functionality, the NSFileManager
API insulates an application from the underlying file system. An important part of this insulation is the encoding of file names (in, for example, Unicode, ISO Latin1, and ASCII). There is a default NSFileManager
object for the file system; this object responds to all messages that request a operation on the associated file system.
The pathnames specified as arguments to NSFileManager
methods can be absolute or relative to the current directory (which you can determine with currentDirectoryPath
and set with changeCurrentDirectoryPath:
). However, pathnames cannot include wildcard characters.
Note: An absolute pathname starts with the root directory of the file system, represented by a slash (/), and ends with the file or directory that the pathname identifies. A relative pathname is relative to the current directory, the directory in which you are working and in which saved files are currently stored (if no pathname is specified). Relative pathnames start with a subdirectory of the current directory—without an initial slash—and end with the name of the file or directory the pathname identifies.
Constructing a pathname to a file does not guarantee that the file exists at that path. Specifying a path results in one of the following possibilities:
A file exists at that path
A link to a file exists at that path
A broken link exists at that path
No file exists at that path
If the pathname specifies a valid file or link, you can obtain information about the file using the methods of this class. If the pathname specifies a broken link, you can still use fileAttributesAtPath:traverseLink:
to obtain attributes for the link itself (by specifying NO
for the traverseLink argument). However, the methods fileExistsAtPath:
and fileAttributesAtPath:traverseLink:
(with YES
specified for the traverseLink argument) return nil
when the pathname specifies a broken link. Other methods return appropriate errors—see the method descriptions for specific information. Regardless of whether a link is broken or valid, the link still appears in directory listings.
NSFileManager
methods are commonly used together with path-utility methods implemented as a category on NSString
. These methods extract the components of a path (directory, file name, and extension), create paths from those components, “translate” path separators, clean up paths containing symbolic links and redundant slashes, and perform similar tasks. Where your code manipulates strings that are part of file-system paths, it should use these methods. See the specification of the NSString
class for details.
© 1997, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-03-05)