2009-11-03 60 views
0

我試圖想出一個辦法這個工作:使用Objective-C複製關閉目錄的網絡驅動器/ C

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

    const char* system_call = [searchCommand UTF8String]; 
    system(system_call); 

系統()方法並不承認特定字符串我通過。如果我嘗試類似:

system("kextstat"); 

沒問題。任何想法爲什麼我傳入的查找字符串命令不起作用?我在xCode中得到的是「GDB:Running .....」。我應該提到,如果我在終端中嘗試相同的確切命令,它的工作原理就很好。

回答

1

看到一個字節,不要使用system()。相反,使用-[NSFileManager moveItemAtPath:toPath:error:]像這樣:

/* 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]; 
} 

如果你不關心是平臺無關的,你可以硬編碼srcdest參數,直接執行移動,並通過75%縮短這個。

請注意,這將而不是做globbing - 它期望有一個名爲「Directory *」的目錄,其中星號是目錄名的最後部分。您可以使用<glob.h>中定義的​​3210自己處理globbing。

0

我不知道Objective-C,但你傳遞給system()

我想嘗試這個,只是一個

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("");