如何重命名目录?

时间:2010-12-20 04:55:01

标签: iphone objective-c nsfilemanager

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

我想通过代码重命名该文件夹,但无法理解如何操作。

请帮帮我。

5 个答案:

答案 0 :(得分:16)

你试过吗?

    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
    }

答案 1 :(得分:7)

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
}

}

答案 2 :(得分:5)

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

-(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 :(得分:1)

这始终有效

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 :(得分:0)

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

// 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