簡體   English   中英

因為我可以將所有文件從Objective-C中的一個目錄移動到另一個目錄?

[英]as I can move all files from one directory to another directory in Objective-C?

因為我可以將所有文件從一個目錄移動到另一個目錄? 我有自己的代碼,因此可以單獨移動它們,但不能全部移動它們。

 NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"user"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];



    NSString *oldPath = [NSString stringWithFormat:@"%@/user/%@", documentsDirectory, fileName];

     NSLog(@"copia de aqui %@", oldPath);
    NSString *newPath = [NSString stringWithFormat:@"%@/leidos/%@", documentsDirectory, fileName];
     NSLog(@"aqui aqui %@", newPath);

    [[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];

您可以在../user/目錄中獲取所有文件,並將它們的文件名存儲到數組中。 然后,您可以遍歷數組並將文件移動到新目錄。

NSString *oldDirectory = [documentsDirectory stringByAppendingPathComponent:@"user"];
NSString *newDirectory = [documentsDirectory stringByAppendingPathComponent:@"leidos"];
NSFileManager *fm = [NSFileManager defaultManager];

// Get all the files at ~/Documents/user
NSArray *files = [fm contentsOfDirectoryAtPath:oldDirectory error:&error];

for (NSString *file in files) {
    [fm moveItemAtPath:[oldDirectory stringByAppendingPathComponent:file]
                toPath:[newDirectory stringByAppendingPathComponent:file]
                 error:&differentError];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM