views:

41

answers:

5

Times like this and my Objective-C noobness shows. :-/

So, the more I work on a routine to do this, the more complex it's becoming, and I'm wondering if there isn't just a simple method to change the name of a filename in a path. Basically, I want to change @"/some/path/abc.txt to @"/some/path/xyz.txt -- replacing the filename portion but not changing the path or extension.

Thanks!

+1  A: 

You can try something like this:

NSRange slashRange = [myString rangeOfString:@"\\" options:NSBackwardsSearch];
NSRange periodRange = [myString rangeOfString:@"." options:NSBackwardsSearch];
NSString *newString = [myString stringByReplacingCharactersInRange:NSMakeRange(slashRange.location, periodRange.location) withString:@"replacement-string-here"];

What this code does is it gets the location of the \ and . characters and performs a backwards search so that it returns the last occurrence of it in the string. Then, it creates a new range based on those previous ranges and replaces the contents in that range with stringByReplacingCharactersInRange:.

Jacob Relkin
@Jacob: did you know there are a load of NSString methods that allow you to manipulate file system paths? In particular, they are path separator agnostic so that you don't have to worry about using the wrong one (as you have here for Apple native file systems).http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-SW38
JeremyP
+1  A: 

Take a look at the "Working With Paths" section of the NSString docs. In particular, lastPathComponent, pathExtension and stringByDeletingPathExtension: should do what you want.

Chuck
+2  A: 

Try the following:

NSString* initPath = ...
NSString *newPath = [[NSString stringWithFormat:@"%@/%@",
                    [initPath stringByDeletingLastPathComponent], newFileName] 
                     stringByAppendingPathExtension:[initPath pathExtension]];
Vladimir
A: 

What Vladimir said, just broken down more to make it a little easier to read:

NSString *pathToFile = @"/Path/To/File.txt";
NSString *oldFileName = [pathToFile lastPathComponent];
NSString *newFileName = [@"Document" stringByAppendingString:[oldFileName pathExtension];
NSString *newPathToFile = [pathToFile stringByDeletingLastPathComponent];
[newPathToFile stringByAppendingString:newFileName];
peelman
Gives: @"/Path/ToDocumenttxt" because your methods for decomposing the old name into components do not preserve the trailing / or the . for the extension.
JeremyP
Doh! Was typing it on here with no CodeSense, forgot about stringByAppendingPathComponent and stringByAppendingPathExtension :)
peelman
A: 

Try this:

NSString* path = [startString stringByDeletingLastPathComponent];
NSString* extension = [startString pathExtension];
NSString* replacementFileName = [@"foo" stringByAppendingPathExtension: extension];
NSString result = [path stringByAppendingPathComponent: replacementFileName];
JeremyP