2010-12-20 13 views
7

Ich habe einen Ordner im Ordner "Dokumente" in meinem Anwendungsverzeichnis erstellt.Wie man Verzeichnisse umbenennt?

Ich wollte diesen Ordner durch Code umbenennen, aber nicht in der Lage zu verstehen, wie es geht.

Bitte helfen Sie mir.

Antwort

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

Entschuldigung, wenn ich irgendwo etwas verpasst habe, aber musst du nicht auch das alte Verzeichnis löschen? –

+0

@Peter Johnson: Wenn Sie Ihr altes Verzeichnis nicht löschen müssen, können Sie denselben Code verwenden, indem Sie die Zeile ersetzen: [[NSFileManager defaultManager] moveItemAtPath: oldFilePath toPath: newFilePath Fehler: & error]; zu [[NSFileManager defaultManager] copyItemAtPath: oldFilePath toPath: newFilePath Fehler: & error] ;. – iPhoneDv

+1

Die von mackross vorgeschlagene Antwort ist viel einfacher und weniger kompliziert. –

15

Haben Sie es versucht?

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

ich versucht habe diese already..it nicht funktioniert .. – iPhoneDev

+0

wie werden Sie den Pfad zu bekommen? – mackross

+0

erhalten Sie auch einen Fehler? NSLog (@ "% @", error.localizedDescription); – mackross

1

Diese arbeiten immer

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

Dieser hat einen Nebeneffekt der Duplizierung der Daten. Wenn die Daten groß sind oder die App mitten in der Operation abstürzt, könnte dies ein Problem verursachen. –

5

moveItemAtPath sollte mit arbeiten. Manchmal wird das Verzeichnis nicht wirklich "umbenannt", sondern wirklich an einen anderen Ort verschoben. In diesem Fall muss auch die Verzeichnisstruktur des Zielpfads erstellt werden. Hier ist ein Code-Snippet ich das bin mit gut funktioniert:

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

Es löscht nicht das alte Verzeichnis..does it? –

0

Dies ist guter Artikel zum Umbenennen, Löschen und Dateien erstellen.

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

Verwandte Themen