Don't use system()
. Instead, use -[NSFileManager moveItemAtPath:toPath:error:]
like so:
/* Find the user's Desktop directory. */
NSArray *results = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES/*expandTilde*/);
if (![results count]) return;
NSString *parentDir = [results objectAtIndex:0U];
/* Build the source path in a platform-independent manner. */
/* -pathWithComponents: will use the separator ('/', '\', or ':')
* appropriate for the platform. */
NSArray *srcParts = [NSArray arrayWithObjects:@"/", @"Volumes", @"Public", @"Directory*", (void *)nil];
NSString *src = [NSString pathWithComponents:srcParts];
/* Build the destination path. */
NSString *fnam = [src lastPathComponent];
NSString *dest = [parentDir stringByAppendingPathComponent:fnam];
/* Move |src| to |dest|. */
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = NULL;
BOOL ok = [fm moveItemAtPath:src toPath:dest error:&error];
if (!ok) {
[[NSApplication sharedApplication] presentError:error];
}
If you don't care about being platform-independent, you can just hardcode the src
and dest
arguments, perform the move directly, and shorten this by 75%.
Note that this will not do globbing – it expects there to be a directory named "Directory*", where the asterisk is the last part of the directory name. You can handle globbing yourself using glob()
, defined in <glob.h>
.