File Management

This article describes how you can perform a number of file and file-related operations.

Moving, Copying, and Deleting Files

The following examples show file manipulation using the URL-based methods of NSFileManager that are available in Mac OS X v10.6 and later.

To move or rename a file or directory, use moveItemAtURL:toURL:error:, as illustrated by this code fragment:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *srcURL = <#Get the source URL#>;
NSURL *destinationURL = <#Create the destination URL#>;
 
NSError *error = nil;
if (![fileManager moveItemAtURL:srcURL toURL:destinationURL error:&error]) {
    // Handle the error.
}

To copy a file or directory, use copyItemAtURL:toURL:error:, as illustrated by this code fragment:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *srcURL = <#Get the source URL#>;
NSURL *destinationURL = <#Create the destination URL#>;
 
NSError *error = nil;
if (![fileManager copyItemAtURL:srcURL toURL:destinationURL error:&error]) {
    // Handle the error.
}

To delete a file or directory, use removeItemAtURL:error:, as illustrated by this code fragment:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *url = <#Get the URL of the item to delete#>;
 
NSError *error = nil;
if (![fileManager removeItemAtURL error:&error]) {
    // Handle the error.
}

You can also replace a file or directory with another file or directory, renaming the original item with a name to signify that it it is a backup using replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: (for Carbon developers, this is a replacement for the FSExchangeObjects function). You typically need this functionality when saving documents; since NSDocument performs this operation itself when saving a document, there may generally be little reason for you to use it yourself.

Finder-Like Operations

In Mac OS X v10.6 and later, NSWorkspace provides two methods you can use to duplicate files or move them to the trash in the same way that Finder does.

Moving Files to the Trash

You use recycleURLs:completionHandler: to move files at the specified URLs to the trash in the same manner as the Finder. The completion handler argument is a block object that is invoked when the operation has finished.

NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSArray *URLs = <#An array of file URLs#>;
[workspace recycleURLs:URLs completionHandler:^(NSDictionary *newURLs, NSError *error) {
    if (error != nil) {
        // Deal with any errors here.
    }
}];

Duplicating Files

You use duplicateURLs:completionHandler: to make copies of files at specified URLs in the same manner as the Finder. The completion handler argument is a block object that is invoked when the operation has finished.

NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSArray *URLs = <#An array of file URLs#>;
[workspace duplicateURLs:URLs completionHandler:^(NSDictionary *newURLs, NSError *error) {
    if (error != nil) {
        // Deal with any errors here.
    }
}];