是否有一种简单的方法来获取给定的文件路径并修改它以避免名称冲突?喜欢的东西:NSString独特的文件路径,以避免名称冲突
[StringUtils stringToAvoidNameCollisionForPath:path];
,对于类型的给定路径:/foo/bar/file.png
,将返回/foo/bar/file-1.png
后来人们都会增加“-1”类似于什么Safari浏览器也对下载的文件。
UPDATE:
我跟着灰沟的建议和我贴我的实现作为答案:)
是否有一种简单的方法来获取给定的文件路径并修改它以避免名称冲突?喜欢的东西:NSString独特的文件路径,以避免名称冲突
[StringUtils stringToAvoidNameCollisionForPath:path];
,对于类型的给定路径:/foo/bar/file.png
,将返回/foo/bar/file-1.png
后来人们都会增加“-1”类似于什么Safari浏览器也对下载的文件。
UPDATE:
我跟着灰沟的建议和我贴我的实现作为答案:)
我决定实施我自己的解决方案,我想分享我的代码。这不是最可取的实施,但似乎做的工作:
+ (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);
}
我也有类似的问题,并用稍宽的方法上来,试图命名文件iTunes会采用同样的方式(当你设置为管理你的音乐库,并且你有多个同名的音轨等)
它工作在一个循环中,所以这个函数可以多次调用并且仍然产生有效的输出。解释参数,fileName
是没有路径或扩展名(例如“文件”)的文件的名称,folder
只是路径(例如“/ foo/bar”),而fileType
只是扩展名(例如“png”) 。这三个可以作为一个字符串传递并在之后分开,但在我的情况下,将它们分开是有意义的。
currentPath
(它可以是空的,但不为零)在重命名文件而不是创建新文件时非常有用。例如,如果您尝试重命名为“/foo/bar/file.png”的“/ foo/bar/file 1.png”,则会传入“/ foo/bar/file 1.png “对于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;
}
甜。这很好。但注意到它的名称是“name 1 2 3.ext”,而不是“name 3.ext”,但是不管怎样。 –
这是我的意图。如果你最后传入一个带有数字的文件名,这就是你想要保留的东西。修改原始文件名末尾的数字和空格会很容易,如果这不是所期望的行为。 – Dov
为什么不使用GUID作为文件名或文件夹名称?检查''[[NSProcessInfo processInfo] globalUniqueString]' – Eimantas
不,我的问题是没有关系使一个字符串唯一(我会在这种情况下使用CFUUIDCreate()),但我想保留文件名并只添加一个后缀它是独一无二的:P – daveoncode
我实现了我自己的解决方案......欢迎评论:) – daveoncode