FileManager 删除文件 移动并重命名文件 获取文件大小

/*删除文件 指向目录为(NSCachesDirectory)临时文件夹*/

- (void)deleteFileAtPath:(NSString *)filename {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];

    NSString *delelteFilePath = [documentsDirectory stringByAppendingPathComponent:filename];

    NSError *error;

    if ([fileManager removeItemAtPath:delelteFilePath error:&error] != YES)

        NSLog(@"Unable to delete file: %@", [error localizedDescription]);

}

/* 移动文件,从NSCachesDirectory 移动到 NSCachesDirectory//tempFolder目录 */

- (void)moveFileAtPath:(NSString *)desFileName SrcFileName:(NSString *)srcFileName {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    //目标目录

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];

    NSString *tempFolderDirDirectory = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",@"tempFolder"]];

    NSString *desFileNamePath = [tempFolderDirDirectory stringByAppendingPathComponent:desFileName];

    BOOL isDir = NO;

    NSError *error;

    [fileManager fileExistsAtPath:desFileNamePath isDirectory:(&isDir)];

    if (!isDir) { //不存在就要创建目标目录

        if ([fileManager createDirectoryAtPath:tempFolderDirDirectory withIntermediateDirectories:YES attributes:nil error:&error] != YES) {

            NSLog(@"Unable to create  file directory: %@", [error localizedDescription]);

        };

    }

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];

    NSString *srcFileNamePath = [documentsDirectory stringByAppendingPathComponent:srcFileName];

    //判断是否移动

    if ([fileManager moveItemAtPath:srcFileNamePath toPath:desFileNamePath error:&error] != YES)

        NSLog(@"Unable to move file: %@", [error localizedDescription]);

}

/* 遍历文件夹的所有文件(非隐藏文件)并移动到指定目录*/

- (void)removeAllFile {

    NSFileManager *fileManager = [NSFileManager defaultManager];

    //在这里获取应用程序Documents文件夹里的文件及文件夹列表

    NSError *error = nil;

    NSArray *fileList = [[NSArray alloc] init];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];

    NSString *tempFolderDirDirectory = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",@"tempFolder"]];

    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组

    fileList = [fileManager contentsOfDirectoryAtPath:tempFolderDirDirectory error:&error];

    

    BOOL isDir = NO;

    for (NSString *fileName in fileList) {

        NSString *filePathName = [tempFolderDirDirectory stringByAppendingPathComponent:fileName];

        //注意过滤隐藏文件(如D.store

        [fileManager fileExistsAtPath:filePathName isDirectory:(&isDir)];

        if (!isDir) {

            [self moveFileAtPath:fileName SrcFileName:fileName];

        }

    }

    [fileList release];

}

/*接口函数  指向目录为(NSCachesDirectory)临时文件夹*/

-(NSString *)myFileFolderFileSize {

    return [NSString stringWithFormat:@"%.2f M",[self folderSizeAtPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0]]];

}


/*单个文件的文件大小*/

- (long long) fileSizeAtPath:(NSString*) filePath{

    NSFileManager* manager = [NSFileManager defaultManager];

    if ([manager fileExistsAtPath:filePath]){

        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

    }

    return 0;

}

/*整个目录的文件大小*/

- (float ) folderSizeAtPath:(NSString*) folderPath{

    NSFileManager* manager = [NSFileManager defaultManager];

    if (![manager fileExistsAtPath:folderPath]) return 0;

    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];

    NSString* fileName;

    long long folderSize = 0;

    while ((fileName = [childFilesEnumerator nextObject]) != nil){

        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

        folderSize += [self fileSizeAtPath:fileAbsolutePath];

    }

    return folderSize/(1024.0*1024.0);

}



你可能感兴趣的:(FileManager 删除文件 移动并重命名文件 获取文件大小)