moveItemAtPath :toPath:error:
Moves the file or directory at the specified path to a new location synchronously.
Parameters
- srcPath
- The path to the file or directory you want to move. This parameter must not be
nil
. - dstPath
- The new path for the item in srcPath. This path must include the name of the file or directory in its new location. This parameter must not be
nil
. - error
- On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify
nil
for this parameter if you do not want the error information.
Return Value of [NSFileManager moveItemAtPath]
YES
if the item was moved successfully or the file manager’s delegate aborted the operation deliberately. Returns NO
if an error occurred.Discussion of [NSFileManager moveItemAtPath]
When moving items, the current process must have permission to read the item atsrcPath and write the parent directory of dstPath. If the item at srcPath is a directory, this method moves the directory and all of its contents, including any hidden files. If an item with the same name already exists at dstPath, this method aborts the move attempt and returns an appropriate error. If the last component of srcPath is a symbolic link, only the link is moved to the new path; the item pointed to by the link remains at its current location.[NSFileManager moveItemAtPath]
Prior to moving the item, the file manager asks its delegate if it should actually move it. It does this by calling the
fileManager:shouldMoveItemAtURL:toURL:
method; if that method is not implemented it calls thefileManager:shouldMoveItemAtPath:toPath:
method instead. If the item being moved is a directory, the file manager notifies the delegate only for the directory itself and not for any of its contents. If the delegate method returns YES
, or if the delegate does not implement the appropriate methods, the file manager moves the file. If there is an error moving one out of several items, the file manager may also call the delegate’sfileManager:shouldProceedAfterError:movingItemAtURL:toURL:
orfileManager:shouldProceedAfterError:movingItemAtPath:toPath:
method to determine how to proceed.
If the source and destination of the move operation are not on the same volume, this method copies the item first and then removes it from its current location. This behavior may trigger additional delegate notifications related to copying and removing individual items.
Example of [NSFileManager moveItemAtPath]
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef uuidString = CFUUIDCreateString(NULL, uuid);
NSString *tmpDir = [[NSTemporaryDirectory() stringByAppendingPathComponent:(NSString *)uuidString];
CFRelease(uuid);
CFRelease(uuidString);
// MOVE IT
[[NSFileManager defaultManager] moveItemAtPath:myDirPath toPath:tmpDir error:&error]