Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to copy or move files with Objective-C?

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!

like image 948
winsmith Avatar asked Jan 29 '09 14:01

winsmith


People also ask

What is the easiest method for copying and moving files?

Select the file you want to copy by clicking on it once. Right-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V .

How do I move files rather than copy?

To move a file, hold down the Shift key while dragging. You can also use the middle mouse button to drag files. In this case, gThumb will ask you if you wish to copy the files, move the files, or cancel the operation. Select the files to be transferred, right-click on the selection, and choose Copy to... or Move to....

Which is better copy or move?

Generally, Moving files will be faster because when moving, it will just change the links, not the Actual Position on the physical device. While copying will actually read and write the information to other place and hence takes more time.


1 Answers

NSFileManager and NSWorkspace both have methods to move, copy, and delete files. Usually you'd use NSFileManager since its easier to work with:

if ( [[NSFileManager defaultManager] isReadableFileAtPath:source] )     [[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:nil]; 

However, NSWorkspace can easily move files to the Trash, which NSFileManager can't do.

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

Check the documentation for a more complete description of the two classes. (NSFileManager, NSWorkspace)

like image 78
Marc Charbonneau Avatar answered Sep 28 '22 05:09

Marc Charbonneau