开发者

Copying a directory off a network drive using Objective-C/C

I'm trying to figure out a way for this to work:

NSString *searchCommand = [[NSString alloc] initWithFormat:@"cp -R /Volumes/Public/DirectoryToCopy* Desktop/"];

    const char* system_开发者_运维百科call = [searchCommand UTF8String];
    system(system_call);

The system() method does not acknowledge the specific string I am passing in. If I try something like:

system("kextstat");

No problems. Any ideas why the find string command I'm passing in is not working? All I get is "GDB: Running ....." in xCode. I should mention that if I try the same exact command in terminal it works just fine.


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>.


I don't know Objective-C, but what are you passing to system()?

I'd try this, just to see the bytes one by one

NSString *searchCommand = [[NSString alloc] initWithFormat:@"cp -R /Volumes/Public/Directory* Desktop/"];

    const char* system_call = [searchCommand UTF8String];
    const char *ptr = system_call;
    while (*ptr) printf("%02X ", *ptr++);
    puts("");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜