views:

3103

answers:

2

I was wondering, is there a best practice to write an OSX programm that copies or moves a file from one place to another?

  • is there some NSSomething method I can call?
  • Do I have to work with Input/Output streams?
  • Or is the best way maybe to just rely on passing commands to the finder?

Bonus question: How do I get percentages a la "copy 12% complete" with one of these methods?

Thanks for your help!

+4  A: 

NSFileManager and NSWorkspace both have methods to move, copy, and delete files. Usually you'd use NSFileManager since its easier to work with, although NSWorkspace can easily move files to the Trash, which NSFileManager can't do. Here are a few quick examples:

if ( [[NSFileManager defaultManager] isReadableFileAtPath:source] )
    [[NSFileManager defaultManager] copyPath:source toPath:destination handler:nil];

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:foldername destination:@"" files:filenamesArray tag:&tag];

Check the documentation for a more complete description of the two classes.

Marc Charbonneau
+2  A: 

[[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:error]

Here's the link to the class reference:

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/copyItemAtPath:toPath:error:

Ed Marty