How to rename directories?

百般思念 提交于 2019-12-20 10:14:05

问题


I have created a folder within the Documents folder in my application directory .

I wanted to rename that folder through code,but not able to understand how to do it.

Please help me out.


回答1:


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:


Have you tried?

    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
    }



回答3:


Using moveItemAtPath should work. Sometimes the directory isn't actually "renamed" but really moved to another place. In which case the target path directory structure needs to be created as well. Here a code snippet i'm using that works well :

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



回答4:


This always work

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



回答5:


This is good article for renaming, deleting and create files.

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



来源:https://stackoverflow.com/questions/4486979/how-to-rename-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!