2010-12-20 150 views
7

我在我的应用程序目录的Documents文件夹中创建了一个文件夹。如何重命名目录?

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

请帮我一把。

回答

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 
} 

} 
+0

道歉,如果我错过了什么地方,但你不需要删除旧的目录吗? – 2011-02-24 00:54:43

+0

@Peter Johnson:如果你不需要删除你的旧目录,那么你可以通过替换下面的代码来使用相同的代码:[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:&error];到[[NSFileManager defaultManager] copyItemAtPath:oldFilePath toPath:newFilePath error:&error] ;. – iPhoneDv 2011-02-25 06:19:57

+1

马克斯罗斯提出的答案要简单得多,不太复杂。 – 2011-10-14 15:22:18

15

你试过了吗?

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 
    } 
+0

我已经试过这already..it不工作.. – iPhoneDev 2010-12-20 05:42:56

+0

你是如何得到的路径? – mackross 2010-12-20 05:43:50

+0

也是你得到一个错误?的NSLog(@ “%@”,error.localizedDescription); – mackross 2010-12-20 05:45:35

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]) {} 
+0

这一个有复制数据的副作用。如果数据很大,或者如果应用程序在操作过程中崩溃,则可能会导致问题。 – 2012-09-27 10:54:34

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); 
} 
+0

它并没有清除旧的目录..是吗? – 2014-05-09 05:33:25

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