【问题标题】:How to rename directories?如何重命名目录?
【发布时间】:2011-05-28 02:00:01
【问题描述】:

我在我的应用程序目录的 Documents 文件夹中创建了一个文件夹。

我想通过代码重命名那个文件夹,但不明白怎么做。

请帮帮我。

【问题讨论】:

    标签: iphone objective-c nsfilemanager


    【解决方案1】:

    这是重命名、删除和创建文件的好文章。

    // For error information
       NSError *error;
    
    // Create file manager
       NSFileManager *fileMgr = [NSFileManager defaultManager];
    
    // Point to Document directory
       NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    // Rename the file, by moving the file
       NSString *filePath2 = [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];
    
    // Attempt the move
       if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
           NSLog(@"Unable to move file: %@", [error localizedDescription]);
    
    // Show contents of Documents directory
         NSLog(@"Documents directory: %@", 
         [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
    

    http://iosdevelopertips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html

    【讨论】:

      【解决方案2】:

      使用 moveItemAtPath 应该可以工作。有时该目录实际上并没有“重命名”,而是真正移动到了另一个地方。在这种情况下,还需要创建目标路径目录结构。 这是我正在使用的代码 sn-p 效果很好:

      -(BOOL)renameDir:(NSString *)dirPath asDir:(NSString *)newDirPath cleanExisting:(BOOL)clean
      {
          NSError *error = nil;
          NSFileManager *fm = [NSFileManager defaultManager];
          if (clean && [fm fileExistsAtPath:newDirPath])
          {
              [fm removeItemAtPath:newDirPath error:&error];
              if (error != nil)
              {
                  NSLog(@"Error while renameDir %@ as %@ :\n%@",dirPath,newDirPath,error);
                  return NO;
              }
          }
          //Make sure container directories exist
          NSString *newDirContainer = [newDirPath stringByDeletingLastPathComponent];
          if (![fm fileExistsAtPath:newDirContainer])
          {
            [fm createDirectoryAtPath:newDirContainer withIntermediateDirectories:YES attributes:nil error:&error];
           }
      
          if (error==nil)
          {
              [fm moveItemAtPath:dirPath toPath:newDirPath error:&error];
          }
          if (error!=nil)
          {
              NSLog(@"error while moveItemAtPath : %@",error);
          }
          return (error==nil);
      }
      

      【讨论】:

      • 它不清理旧目录..是吗?
      【解决方案3】:

      这总是有效的

      NSLog (@"Copying download file from %@ to %@", aPath, bPath);
      if ([[NSFileManager defaultManager] fileExistsAtPath: bPath]) {
                  [[NSFileManager defaultManager] removeItemAtPath: bPath
                                                             error: &error];
              }
      
      if (![[NSFileManager defaultManager] copyItemAtPath: aPath
                                                           toPath: bPath
                                                            error: &error]){}
      if ([[NSFileManager defaultManager] removeItemAtPath: aPath
                                                             error: &error]) {}
      

      【讨论】:

      • 这个有复制数据的副作用。如果数据很大,或者应用程序在操作过程中崩溃,这可能会导致问题。
      【解决方案4】:
      NSString *oldDirectoryPath = @"Type your old directory Path";
      
      NSArray *tempArrayForContentsOfDirectory =[[NSFileManager defaultManager] contentsOfDirectoryAtPath:oldDirectoryPath error:nil];
      
      NSString *newDirectoryPath = [[oldDirectoryPath stringByDeletingLastPathComponent]stringByAppendingPathComponent:newDirectoryname];
      
      [[NSFileManager defaultManager] createDirectoryAtPath:newDirectoryPath attributes:nil];
      
      for (int i = 0; i < [tempArrayForContentsOfDirectory count]; i++)
      {
      
      NSString *newFilePath = [newDirectoryPath stringByAppendingPathComponent:[tempArrayForContentsOfDirectory objectAtIndex:i]];
      
      NSString *oldFilePath = [oldDirectoryPath stringByAppendingPathComponent:[tempArrayForContentsOfDirectory objectAtIndex:i]];
      
      NSError *error = nil;
      [[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:&error];
      
      if (error) {
       // handle error
      }
      
      }
      

      【讨论】:

      • 抱歉,如果我在某处遗漏了什么,但您不需要删除旧目录吗?
      • @Peter Johnson:如果您不需要删除旧目录,则可以通过替换以下行来使用相同的代码:[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:&error ];到 [[NSFileManager defaultManager] copyItemAtPath:oldFilePath toPath:newFilePath error:&error];.
      • maccross 提出的答案要简单得多,也不那么复杂。
      【解决方案5】:

      你试过了吗?

          NString *newDirectoryName = @"<new folder name>";    
          NSString *oldPath = @"<path to the old folder>";
          NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newDirectoryName];
          NSError *error = nil;
          [[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
          if (error) {
              NSLog(@"%@",error.localizedDescription);
              // handle error
          }
      

      【讨论】:

      • 你是怎么得到路径的?
      • 您是否也遇到了错误? NSLog(@"%@",error.localizedDescription);
      • 你的路径是否包含 Documents 目录路径?
      • 这很好用,而且比上面给出的例子更简单。这应该被标记为正确答案。 +1
      • 也许这已经太旧了,但我收到一个错误:操作无法完成。 (Cocoa 错误 4。)有关如何解决此问题的任何建议?
      猜你喜欢
      • 2012-02-03
      • 2019-01-23
      • 1970-01-01
      • 2011-07-15
      • 2015-10-08
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多