NSString 唯一文件路径以避免名称冲突

发布于 2024-12-01 15:01:10 字数 317 浏览 2 评论 0原文

有没有一种简单的方法来获取给定的文件路径并修改它以避免名称冲突?类似于:

[StringUtils stringToAvoidNameCollisionForPath:path];

对于给定类型的路径:/foo/bar/file.png,将返回/foo/bar/file-1.png,稍后它将返回递增“-1”,类似于 Safari 对下载文件所做的操作。

更新:

我遵循了 Ash Furrow 的建议,并发布了我的实现作为答案:)

Is there a simple way to take a given file path and modify it in order to avoid name collisions? Something like:

[StringUtils stringToAvoidNameCollisionForPath:path];

that for a given path of type: /foo/bar/file.png, will return /foo/bar/file-1.png and later it will increment that "-1" similarly to what Safari does for downloaded files.

UPDATE:

I followed Ash Furrow's suggestion and I posted my implementation as answer :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぇ气 2024-12-08 15:01:10

我遇到了类似的问题,并提出了一种稍微更广泛的方法,尝试以 iTunes 相同的方式命名文件(当您将其设置为管理您的资料库并且您有多个具有相同名称的曲目等时)

。在循环中,因此可以多次调用该函数并仍然产生有效的输出。解释参数,fileName 是没有路径或扩展名的文件名(例如“file”),folder 只是路径(例如“/foo/bar” ),fileType 只是扩展名(例如“png”)。这三个可以作为一个字符串传入,然后再分开,但就我而言,将它们分开是有意义的。

currentPath(可以为空,但不能为 nil)在重命名文件而不是创建新文件时很有用。例如,如果您尝试将“/foo/bar/file 1.png”重命名为“/foo/bar/file.png”,则应传入“/foo/bar/file 1.png” ” for currentPath,如果“/foo/bar/file.png”已经存在,您将返回开始时的路径,而不是看到“/foo/bar/file 1. png”并返回“/foo/bar/file 2.png"

+ (NSString *)uniqueFile:(NSString *)fileName
                inFolder:(NSString *)folder
           withExtension:(NSString *)fileType
        mayDuplicatePath:(NSString *)currentPath
{
    NSUInteger existingCount = 0;
    NSString *result;
    NSFileManager *manager = [NSFileManager defaultManager];

    do {
        NSString *format = existingCount > 0 ? @"%@ %lu" : @"%@";

        fileName = [NSString stringWithFormat:format, fileName, existingCount++];
        result = [fileName stringByAppendingFormat:@".%@", [fileType lowercaseString]];

        result = [folder stringByAppendingPathComponent:result];
    } while ([manager fileExistsAtPath:result] &&
             // This comparison must be case insensitive, as the file system is most likely so
             [result caseInsensitiveCompare:currentPath] != NSOrderedSame);

    return result;
}

I had a similar problem, and came up with a slightly broader approach, that attempts to name files the same way iTunes would (when you have it set to manage your library and you have multiple tracks with the same name, etc.)

It works in a loop, so the function can be called multiple times and still produce valid output. Explaining the arguments, fileName is the name of the file with no path or extension (e.g. "file"), folder is just the path (e.g. "/foo/bar"), and fileType is just the extension (e.g. "png"). These three could be passed in as one string and be split out after, but in my case it made sense to separate them.

currentPath (which can be empty, but not nil), is useful when you're renaming a file, not creating a new one. For example, if you have "/foo/bar/file 1.png" that you're trying to rename to "/foo/bar/file.png", you would pass in "/foo/bar/file 1.png" for currentPath, and if "/foo/bar/file.png" already exists, you'll get back the path you started with, instead of seeing that "/foo/bar/file 1.png" and returning "/foo/bar/file 2.png"

+ (NSString *)uniqueFile:(NSString *)fileName
                inFolder:(NSString *)folder
           withExtension:(NSString *)fileType
        mayDuplicatePath:(NSString *)currentPath
{
    NSUInteger existingCount = 0;
    NSString *result;
    NSFileManager *manager = [NSFileManager defaultManager];

    do {
        NSString *format = existingCount > 0 ? @"%@ %lu" : @"%@";

        fileName = [NSString stringWithFormat:format, fileName, existingCount++];
        result = [fileName stringByAppendingFormat:@".%@", [fileType lowercaseString]];

        result = [folder stringByAppendingPathComponent:result];
    } while ([manager fileExistsAtPath:result] &&
             // This comparison must be case insensitive, as the file system is most likely so
             [result caseInsensitiveCompare:currentPath] != NSOrderedSame);

    return result;
}
画中仙 2024-12-08 15:01:10

我决定实现我自己的解决方案,并且我想分享我的代码。这不是最理想的实现,但它似乎可以完成工作:

+ (NSString *)stringToAvoidNameCollisionForPath:(NSString *)path {

    // raise an exception for invalid paths
    if (path == nil || [path length] == 0) {
        [NSException raise:@"DMStringUtilsException" format:@"Invalid path"];
    }

    NSFileManager *manager = [[[NSFileManager alloc] init] autorelease];
    BOOL isDirectory;

    // file does not exist, so the path doesn't need to change
    if (![manager fileExistsAtPath:path isDirectory:&isDirectory]) {
        return path;
    }

    NSString *lastComponent = [path lastPathComponent];
    NSString *fileName = isDirectory ? lastComponent : [lastComponent stringByDeletingPathExtension];
    NSString *ext = isDirectory ? @"" : [NSString stringWithFormat:@".%@", [path pathExtension]];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-([0-9]{1,})$" options:0 error:nil];
    NSArray *matches = [regex matchesInString:fileName options:0 range:STRING_RANGE(fileName)];

    // missing suffix... start from 1 (foo-1.ext) 
    if ([matches count] == 0) {
        return [NSString stringWithFormat:@"%@-1%@", fileName, ext];
    }

    // get last match (theoretically the only one due to "$" in the regex)
    NSTextCheckingResult *result = (NSTextCheckingResult *)[matches lastObject];

    // extract suffix value
    NSUInteger counterValue = [[fileName substringWithRange:[result rangeAtIndex:1]] integerValue];

    // remove old suffix from the string
    NSString *fileNameNoSuffix = [fileName stringByReplacingCharactersInRange:[result rangeAtIndex:0] withString:@""];

    // return the path with the incremented counter suffix
    return [NSString stringWithFormat:@"%@-%i%@", fileNameNoSuffix, counterValue + 1, ext];
}

...以下是我使用的测试:

- (void)testStringToAvoidNameCollisionForPath {

    NSBundle *bundle = [NSBundle bundleForClass:[self class]];  

    // bad configs //

    STAssertThrows([DMStringUtils stringToAvoidNameCollisionForPath:nil], nil);
    STAssertThrows([DMStringUtils stringToAvoidNameCollisionForPath:@""], nil);

    // files //

    NSString *path = [bundle pathForResource:@"bar-0.abc" ofType:@"txt"];
    NSString *savePath = [DMStringUtils stringToAvoidNameCollisionForPath:path];
    STAssertEqualObjects([savePath lastPathComponent], @"bar-0.abc-1.txt", nil);

    NSString *path1 = [bundle pathForResource:@"bar1" ofType:@"txt"];
    NSString *savePath1 = [DMStringUtils stringToAvoidNameCollisionForPath:path1];
    STAssertEqualObjects([savePath1 lastPathComponent], @"bar1-1.txt", nil);

    NSString *path2 = [bundle pathForResource:@"bar51.foo.yeah1" ofType:@"txt"];
    NSString *savePath2 = [DMStringUtils stringToAvoidNameCollisionForPath:path2];
    STAssertEqualObjects([savePath2 lastPathComponent], @"bar51.foo.yeah1-1.txt", nil);

    NSString *path3 = [path1 stringByDeletingLastPathComponent];
    NSString *savePath3 = [DMStringUtils stringToAvoidNameCollisionForPath:[path3 stringByAppendingPathComponent:@"xxx.zip"]];
    STAssertEqualObjects([savePath3 lastPathComponent], @"xxx.zip", nil);

    NSString *path4 = [bundle pathForResource:@"foo.bar1-1-2-3-4" ofType:@"txt"];
    NSString *savePath4 = [DMStringUtils stringToAvoidNameCollisionForPath:path4];
    STAssertEqualObjects([savePath4 lastPathComponent], @"foo.bar1-1-2-3-5.txt", nil);

    NSString *path5 = [bundle pathForResource:@"bar1-1" ofType:@"txt"];
    NSString *savePath5 = [DMStringUtils stringToAvoidNameCollisionForPath:path5];
    STAssertEqualObjects([savePath5 lastPathComponent], @"bar1-2.txt", nil);

    // folders //

    NSString *path6 = [DOCUMENTS_PATH stringByAppendingPathComponent:@"foo1"];
    NSString *savePath6 = [DMStringUtils stringToAvoidNameCollisionForPath:path6];
    STAssertEqualObjects([savePath6 lastPathComponent], @"foo1-1", nil);

    NSString *path7 = [DOCUMENTS_PATH stringByAppendingPathComponent:@"bar1-1"];
    NSString *savePath7 = [DMStringUtils stringToAvoidNameCollisionForPath:path7];
    STAssertEqualObjects([savePath7 lastPathComponent], @"bar1-2", nil);

    NSString *path8 = [DOCUMENTS_PATH stringByAppendingPathComponent:@"foo-5.bar123"];
    NSString *savePath8 = [DMStringUtils stringToAvoidNameCollisionForPath:path8];
    STAssertEqualObjects([savePath8 lastPathComponent], @"foo-5.bar123-1", nil);

}

I decided to implement my own solution and I want to share my code. It's not the most desirable implementation, but it seems to do the job:

+ (NSString *)stringToAvoidNameCollisionForPath:(NSString *)path {

    // raise an exception for invalid paths
    if (path == nil || [path length] == 0) {
        [NSException raise:@"DMStringUtilsException" format:@"Invalid path"];
    }

    NSFileManager *manager = [[[NSFileManager alloc] init] autorelease];
    BOOL isDirectory;

    // file does not exist, so the path doesn't need to change
    if (![manager fileExistsAtPath:path isDirectory:&isDirectory]) {
        return path;
    }

    NSString *lastComponent = [path lastPathComponent];
    NSString *fileName = isDirectory ? lastComponent : [lastComponent stringByDeletingPathExtension];
    NSString *ext = isDirectory ? @"" : [NSString stringWithFormat:@".%@", [path pathExtension]];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"-([0-9]{1,})$" options:0 error:nil];
    NSArray *matches = [regex matchesInString:fileName options:0 range:STRING_RANGE(fileName)];

    // missing suffix... start from 1 (foo-1.ext) 
    if ([matches count] == 0) {
        return [NSString stringWithFormat:@"%@-1%@", fileName, ext];
    }

    // get last match (theoretically the only one due to "$" in the regex)
    NSTextCheckingResult *result = (NSTextCheckingResult *)[matches lastObject];

    // extract suffix value
    NSUInteger counterValue = [[fileName substringWithRange:[result rangeAtIndex:1]] integerValue];

    // remove old suffix from the string
    NSString *fileNameNoSuffix = [fileName stringByReplacingCharactersInRange:[result rangeAtIndex:0] withString:@""];

    // return the path with the incremented counter suffix
    return [NSString stringWithFormat:@"%@-%i%@", fileNameNoSuffix, counterValue + 1, ext];
}

... and the following are the tests I used:

- (void)testStringToAvoidNameCollisionForPath {

    NSBundle *bundle = [NSBundle bundleForClass:[self class]];  

    // bad configs //

    STAssertThrows([DMStringUtils stringToAvoidNameCollisionForPath:nil], nil);
    STAssertThrows([DMStringUtils stringToAvoidNameCollisionForPath:@""], nil);

    // files //

    NSString *path = [bundle pathForResource:@"bar-0.abc" ofType:@"txt"];
    NSString *savePath = [DMStringUtils stringToAvoidNameCollisionForPath:path];
    STAssertEqualObjects([savePath lastPathComponent], @"bar-0.abc-1.txt", nil);

    NSString *path1 = [bundle pathForResource:@"bar1" ofType:@"txt"];
    NSString *savePath1 = [DMStringUtils stringToAvoidNameCollisionForPath:path1];
    STAssertEqualObjects([savePath1 lastPathComponent], @"bar1-1.txt", nil);

    NSString *path2 = [bundle pathForResource:@"bar51.foo.yeah1" ofType:@"txt"];
    NSString *savePath2 = [DMStringUtils stringToAvoidNameCollisionForPath:path2];
    STAssertEqualObjects([savePath2 lastPathComponent], @"bar51.foo.yeah1-1.txt", nil);

    NSString *path3 = [path1 stringByDeletingLastPathComponent];
    NSString *savePath3 = [DMStringUtils stringToAvoidNameCollisionForPath:[path3 stringByAppendingPathComponent:@"xxx.zip"]];
    STAssertEqualObjects([savePath3 lastPathComponent], @"xxx.zip", nil);

    NSString *path4 = [bundle pathForResource:@"foo.bar1-1-2-3-4" ofType:@"txt"];
    NSString *savePath4 = [DMStringUtils stringToAvoidNameCollisionForPath:path4];
    STAssertEqualObjects([savePath4 lastPathComponent], @"foo.bar1-1-2-3-5.txt", nil);

    NSString *path5 = [bundle pathForResource:@"bar1-1" ofType:@"txt"];
    NSString *savePath5 = [DMStringUtils stringToAvoidNameCollisionForPath:path5];
    STAssertEqualObjects([savePath5 lastPathComponent], @"bar1-2.txt", nil);

    // folders //

    NSString *path6 = [DOCUMENTS_PATH stringByAppendingPathComponent:@"foo1"];
    NSString *savePath6 = [DMStringUtils stringToAvoidNameCollisionForPath:path6];
    STAssertEqualObjects([savePath6 lastPathComponent], @"foo1-1", nil);

    NSString *path7 = [DOCUMENTS_PATH stringByAppendingPathComponent:@"bar1-1"];
    NSString *savePath7 = [DMStringUtils stringToAvoidNameCollisionForPath:path7];
    STAssertEqualObjects([savePath7 lastPathComponent], @"bar1-2", nil);

    NSString *path8 = [DOCUMENTS_PATH stringByAppendingPathComponent:@"foo-5.bar123"];
    NSString *savePath8 = [DMStringUtils stringToAvoidNameCollisionForPath:path8];
    STAssertEqualObjects([savePath8 lastPathComponent], @"foo-5.bar123-1", nil);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文