Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename file in Cocoa?

Tags:

How would I rename a file, keeping the file in the same directory?

I have a string containing a full path to a file, and a string containing a the new filename (and no path), for example:

NSString *old_filepath = @"/Volumes/blah/myfilewithrubbishname.avi"; NSString *new_filename = @"My Correctly Named File.avi"; 

I know about NSFileManager's movePath:toPath:handler: method, but I cannot workout how to construct the new file's path..

Basically I'm looking for the equivalent to the following Python code:

>>> import os >>> old_filepath = "/Volumes/blah/myfilewithrubbishname.avi" >>> new_filename = "My Correctly Named File.avi" >>> dirname = os.path.split(old_filepath)[0] >>> new_filepath = os.path.join(dirname, new_filename) >>> print new_filepath /Volumes/blah/My Correctly Named File.avi >>> os.rename(old_filepath, new_filepath) 
like image 921
dbr Avatar asked May 16 '09 23:05

dbr


People also ask

How do I rename a file in sandbox?

To rename a file or folder, in the Sandbox Explorer window, right-click a file; then click Rename.... In the Rename File in Repository dialog box, in the New name field, type the new name of the file or folder and click OK.

How do I rename a file in Xcode?

Right-click on the class name in the interface ( . h ) file, choose Refactor->Rename , and Xcode will guide you through the process, allowing you to preview changes before you commit to them. Xcode will let you review the changes before you commit.

How do I rename a file in Swift?

Renaming Swift codeThe renaming refactoring option can be selected from the refactoring menu by right-clicking the piece of Swift code you want to rename. It will open a new edit overview which will show all the references which will be renamed across multiple files.


2 Answers

NSFileManager and NSWorkspace both have file manipulation methods, but NSFileManager's - (BOOL)movePath:(NSString *)source toPath:(NSString *)destination handler:(id)handler is probably your best bet. Use NSString's path manipulation methods to get the file and folder names right. For example,

NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename]; [[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil]; 

Both classes are explained pretty well in the docs, but leave a comment if there's anything you don't understand.

like image 70
Marc Charbonneau Avatar answered Sep 29 '22 14:09

Marc Charbonneau


It's worth noting that moving a file to itself will fail. I had a method that replaced spaces with underscores and made the file name lowercase and renamed the file to the new name. Files with only one word in the name would fail the rename as the new name would be identical on a case-insensitive file system.

The way I resolved this was to do a two step rename, first renaming the file to a temporary name and then renaming it to the intended name.

Some pseudocode explaining this:

NSString *source = @"/FILE.txt"; NSString *newName = [[source lastPathComponent] lowercaseString]; NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];  [[NSFileManager defaultManager] movePath:source toPath:target error:nil]; // <-- FAILS 

The solution:

NSString *source = @"/FILE.txt"; NSString *newName = [[source lastPathComponent] lowercaseString];  NSString *temp = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-temp", newName]]; NSString *target = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newName];  [[NSFileManager defaultManager] movePath:source toPath:temp error:nil]; [[NSFileManager defaultManager] movePath:temp toPath:target error:nil]; 
like image 21
Martin Gordon Avatar answered Sep 29 '22 14:09

Martin Gordon