hpique
hpique

Reputation: 120334

Add suffix to file name before extension

What's the simplest way to add a suffix to a file name before the extension in Objective-C?

Example

Upvotes: 4

Views: 4336

Answers (4)

Geri Borbás
Geri Borbás

Reputation: 16598

Using URL methods:

let suffixedFileURL = fileURL
    .deletingLastPathComponent()
    .appendingPathComponent(
        fileURL
            .deletingPathExtension()
            .lastPathComponent
            .appending("suffix")
    )
    .appendingPathExtension(fileURL.pathExtension)

Upvotes: 2

javierx
javierx

Reputation: 36

The above solution wouldn't work for filename such as ../filename.pvr.ccz

So I've tweaked it a bit to account for multiple filename extensions:

-(NSString *)appendSuffixToPath:(NSString *)pPath withSuffix:(NSString *)pSuffix {
    NSString * containingFolder = [pPath stringByDeletingLastPathComponent];
    NSString * fullFileName = [pPath lastPathComponent];

    NSMutableArray *extensions = [[NSMutableArray alloc] init];
    NSString * fileExtension = [fullFileName pathExtension];
    [extensions addObject:fileExtension];

    NSString * fileName = [fullFileName stringByDeletingPathExtension];
    while([fileName pathExtension].length > 0){
        fileExtension = [fileName pathExtension];
        [extensions addObject:fileExtension];
        fileName = [fileName stringByDeletingPathExtension];
    }

    NSString * newFileName = [fileName stringByAppendingString:pSuffix];

    NSString * newFullFileName = newFileName;
    while ([extensions count] > 0) {
        newFullFileName = [newFullFileName stringByAppendingPathExtension:[extensions objectAtIndex:((int)[extensions count] - 1)]];
        [extensions removeObjectAtIndex:((int)[extensions count] - 1)];
    }

    NSString *result = [containingFolder stringByAppendingPathComponent:newFullFileName];
    return result;
}

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243156

NSString has a whole bunch of path-related methods:

NSString * appendSuffixToPath(NSString * path, NSString * suffix) {
    NSString * containingFolder = [path stringByDeletingLastPathComponent];
    NSString * fullFileName = [path lastPathComponent];
    NSString * fileExtension = [fullFileName pathExtension];
    NSString * fileName = [fullFileName stringByDeletingPathExtension];
    NSString * newFileName = [fileName stringByAppendingString:suffix];
    NSString * newFullFileName = [newFileName stringByAppendingPathExtension:fileExtension];

    return [containingFolder stringByAppendingPathComponent:newFullFileName];
}

(Yes, you could do that with a lot fewer variables, but I wanted to make it clear what each method was doing)

Upvotes: 14

Chris
Chris

Reputation: 335

know it's old but thought someone may like this Swift solution

extension String {
func appendSuffixBeforeExtension(suffix: String) -> String {
        let regex = NSRegularExpression(pattern: "(\\.\\w+$)", options: nil, error: nil)
        return regex!.stringByReplacingMatchesInString(self, options: nil, range: NSMakeRange(0, count(self)), withTemplate: "\(suffix)$1")
    }
}

Then call it like so

let mytxt = "/somepath/file.jpg"
let retina = mytxt.appendSuffixBeforeExtension("@x2")

Upvotes: 2

Related Questions