2013-02-27 50 views
1

我想在我的iOS應用程序內OSX行爲複製文件時複製文件的NSFileManager:如果存在,計數器追加到它:如果存在

  • myFile.png => MYFILE 1巴紐
  • myFile20.png => myFile20 1.png
  • MYFILE 20.png => MYFILE 21.png
  • ...

是否有一個內置的方式做到這一點還是我必須建立這個函數嗎?自己呢?我找不到像這樣的東西,但我不認爲我是第一個需要這種行爲的人......

+1

你必須自己做。 – trojanfoe 2013-02-27 08:48:07

回答

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; 
    } 
} 

如果該文件不存在,只需使用NSFileManager中的moveItemAtURLmoveItemAtPath將其移動到正確的位置。

0

我不認爲NSFileManager中存在任何內置機制。很可能你需要通過檢查錯誤類型來實現你自己的。如果錯誤是'已經存在',那麼嘗試追加適當的計數器並複製。

0

它不支持iOS。

您將需要生成隨機計數器(可能是時間滴答)並將其附加到文件名。

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; 
} 

如果你願意,你可以添加錯誤消息也

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; 
} 
相關問題