7
votes

Comment renommer des annuaires?

J'ai créé un dossier dans le dossier Documents de mon répertoire d'applications.

Je voulais renommer ce dossier par le code, mais pas capable de comprendre comment le faire.

Aide-moi s'il vous plaît.


0 commentaires

5 Réponses :


16
votes

Avez-vous essayé? XXX


7 commentaires

Comment obtenez-vous le chemin?


Vous obtenez également une erreur? Nslog (@ "% @", error.Localisatedescription);


Votre chemin incluait-il le chemin du répertoire de documents?


Cela fonctionne bien et est beaucoup moins compliqué que l'exemple donné ci-dessus. Cela aurait dû être marqué comme la bonne réponse. +1


Peut-être que c'est déjà trop vieux mais je reçois une erreur: l'opération n'a pas pu être complétée. (Erreur de cacao 4.) Toute suggestion sur la manière de résoudre ce problème?


NsfilenosuchfileError = 4, // Tentative de fonctionnement du système de fichiers sur un fichier inexistant


C'est la meilleure solution.



7
votes
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
}

}

3 commentaires

Toutes mes excuses si j'ai raté quelque chose quelque part quelque part, mais vous n'avez pas besoin de supprimer l'ancien répertoire aussi?


@Peter Johnson: Si vous n'avez pas besoin de supprimer votre ancien répertoire, vous pouvez utiliser le même code en remplaçant la ligne: [[[[[[NsfileManager par défaut] mobileMatPath: OldFilePath Topath: NEWFILEPATH ERREUR: & Erreur]; à [[NsfileManager par défaut] CopétamatPath: OldFilePath Topath: NEWFILEPATH ERREUR: & Erreur] ;.


La réponse suggérée par Mackross est beaucoup plus simple et moins compliquée.



1
votes

Ceci fonctionne toujours

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


1 commentaires

Celui-ci a un effet secondaire de la duplication des données. Si les données sont grandes ou si l'application se bloque au milieu de l'opération, cela pourrait causer un problème.



5
votes

Utiliser mobileMatPath devrait fonctionner. Parfois, le répertoire n'est pas réellement "renommé" mais s'est vraiment déplacé vers un autre endroit. Auquel cas la structure de répertoire de chemin cible doit également être créée. Ici, un extrait de code que j'utilise cela fonctionne bien:

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


1 commentaires

Il ne nettoie pas l'ancien répertoire ...



0
votes

C'est un bon article pour le renommer, la suppression et la création de fichiers. xxx

http://iosdevelopertaps.com/data-file-management/iphone-file-system-Créating-renaming-and-deting-files.html


0 commentaires