7

アプリケーション ディレクトリの Documents フォルダ内にフォルダを作成しました。

コードでそのフォルダーの名前を変更したかったのですが、その方法を理解できませんでした。

私を助けてください。

4

5 に答える 5

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
    }
于 2010-12-20T05:10:41.720 に答える
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
}

}
于 2010-12-20T05:42:06.763 に答える
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);
}
于 2012-09-27T10:51:51.583 に答える
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]) {}
于 2012-01-25T13:25:56.007 に答える
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

于 2013-06-22T10:14:48.430 に答える