2010-12-20 11 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

私はどこかで何かを見逃してもお詫びしますが、古いディレクトリも削除する必要はありませんか? –

+0

@Peter Johnson:古いディレクトリを削除する必要がなければ、同じコードを使用して、[[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:&error];という行を置き換えることができます。 [[NSFileManager defaultManager] copyItemAtPath:oldFilePath toPath:newFilePath error:&error];にコピーします。 – iPhoneDv

+1

mackrossが提案した答えははるかに直接的で、あまり複雑ではありません。 –

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

私は – iPhoneDev

+0

はどのようにパスを取得している。これalready..itが動作していない試してみましたが? – mackross

+0

また、あなたにエラーが発生していますか? NSLog(@ "%@"、error.localizedDescription); – mackross

1

これは、常にmoveItemAtPathが動作するはず使用

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

これは、データを複製する副作用があります。データが大きい場合や、操作の途中でアプリがクラッシュした場合は、問題が発生する可能性があります。 –

5

を働きます。ディレクトリは実際には「名前が変更された」というわけではありませんが、本当に別の場所に移動されることがあります。この場合、ターゲットパスのディレクトリ構造も作成する必要があります。ここで 私はそれがうまく機能使用しているコードスニペット:

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

古いディレクトリをクリーニングしません。 –

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

関連する問題