iOS 创建文件夹,删除文件夹,对文件夹重命名的操作

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

+ (void)createFolder:(NSString *)folderName {
    NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),folderName];
    NSLog(@"HomeDir: %@",imageDir);
    BOOL isDir = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

+ (void)deleteFolder:(NSString *)folderName {
    NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),folderName];
    NSLog(@"HomeDir: %@",imageDir);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:imageDir error:nil];
}

+ (void)changeFolderName:(NSString *)folderName beforeName:(NSString *)beforeName {
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *beforeFolder = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),beforeName];
    NSString *afterFolder = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),folderName];
   
    [fm createDirectoryAtPath:afterFolder withIntermediateDirectories:YES attributes:nil error:nil];
   
    NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:beforeFolder];
    NSString *path;
    while ((path = [dirEnum nextObject]) != nil) {
        [fm moveItemAtPath:[NSString stringWithFormat:@"%@/%@",beforeFolder,path]
                    toPath:[NSString stringWithFormat:@"%@/%@",afterFolder,path]
                     error:NULL];
    }
    [fm removeItemAtPath:beforeFolder error:nil];
}