1

复制文件时,我想在我的 iOS 应用程序中使用 OSX 行为:如果存在,请在其上附加一个计数器:

  • 我的文件.png => 我的文件 1.png
  • 我的文件20.png => 我的文件20 1.png
  • 我的文件 20.png => 我的文件 21.png
  • ...

有没有内置的方法可以做到这一点,还是我必须自己构建该功能?我找不到类似的东西,但我不认为我是第一个需要这种行为的人......

4

5 回答 5

2

您可以执行以下操作,requestedName用户选择的文件名在哪里,文件extension的扩展名basePath是您尝试将其存储在的文件夹:

 NSFileManager *fm = [NSFileManager defaultManager];
 NSString * filename = requestedName;

 if ([fm fileExistsAtPath:[basePath stringByAppendingPathComponent:requestedName]]) 
{   
     unsigned int counter = 1;
     while ( [fm fileExistsAtPath: [basePath stringByAppendingPathComponent: filename]]) 
     {
         //NSLog(@"File already exists %@", filename);
         NSURL *originalFilePath = [NSURL URLWithString:[kTempPath stringByAppendingPathComponent:filename]];
         filename = [[NSString stringWithFormat: @"%@-%d",
         [requestedName stringByDeletingPathExtension], counter]stringByAppendingPathExtension: extension];
         counter ++;
         NSURL *newFilePath = [NSURL URLWithString:[kTempPath stringByAppendingPathComponent:filename]];
         [fm moveItemAtURL:originalFilePath toURL:newFilePath error:nil]; 
      // just in case
         if (counter > 512) break;
     }
}

如果文件不存在,那么只需使用NSFileManagermoveItemAtURLmoveItemAtPath从 NSFileManager 将其移动到正确的位置。

于 2013-02-27T09:08:57.027 回答
1

您可以只更改源文件名,并将该名称用作源路径最后一个组件。像这样:

-(XMPFile*) duplicateDocument:(XMPFile*)file error:(NSError **)error
{
    NSString *filePath = [file path];
    NSString *destinationPath = [[file path] stringByDeletingLastPathComponent];
    NSString *pathExtension = [filePath pathExtension];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *fileName = [[[file path] lastPathComponent] stringByDeletingPathExtension];

    unsigned int counter = 2;

    while ([fileManager fileExistsAtPath:filePath])
    {
        NSString *duplicatedFilePathLastComponent = [[NSString stringWithFormat: @"%@ %d", fileName, counter] stringByAppendingPathExtension: pathExtension];

        NSString *duplicatedFilePath = [destinationPath stringByAppendingPathComponent:duplicatedFilePathLastComponent];

        if (![fileManager fileExistsAtPath:destinationPath]) {
            if ([fileManager copyItemAtPath:filePath toPath:duplicatedFilePath error:error]) {
                XMPFile *duplicatedXMPFile = [[XMPFile alloc] initWithPath:duplicatedFilePath];
                return duplicatedXMPFile;
            }
        }

        counter ++;
    }
    return nil;
}

如果需要,您也可以添加错误消息

于 2014-10-17T01:36:26.443 回答
0

I don't think there is any in built mechanism in NSFileManager. Most probably you will need to implement your own by inspecting error type. If error is of kind 'already exists' then try to append appropriate counter and copy.

于 2013-02-27T08:53:44.690 回答
0

It is not supported by iOS.

You will need to generate the random counter (probably time tick) and append it to file name.

于 2013-02-27T08:53:53.337 回答
0

我意识到这里已经有几个答案,但我觉得我的答案比提供的答案更直接。

我也需要解决这个问题。我刚刚定义了一个函数,以便可以轻松地对其进行单元测试。

- (NSString*)findNextValidFilePathAt:(NSString*)destination
                        withFileName:(NSString*)fileName
{
    NSFileManager* filemgr = [NSFileManager defaultManager];
    NSString* newFilePath = [destination stringByAppendingPathComponent:fileName];
    NSString* originalFilePathWithoutExtension = [newFilePath stringByDeletingPathExtension];
    NSString* pathExtension = [fileName pathExtension];
    int numberToAppend = 0;

    while ([filemgr fileExistsAtPath:newFilePath] && numberToAppend <= 99)
    {
        // find a new file path to try: <original_path> <number>.<extension>
        // Ex: /users/alex/desktop/image 1.png
        numberToAppend++;
        newFilePath = [NSString stringWithFormat:@"%@ %d.%@",
                       originalFilePathWithoutExtension,
                       numberToAppend,
                       pathExtension];
    }

    return newFilePath;
}
于 2017-05-19T17:49:26.677 回答