ios遍历计算文件夹下文件总大小

来源:互联网 发布:音速启动是什么软件 编辑:程序博客网 时间:2024/04/30 09:35

计算文件夹size

-(long)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小

{

    NSFileManager *fileManager = [[NSFileManager alloc] init];

    

    NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];

    for(int i = 0; i<[array count]; i++)

    {

        NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];

        

        BOOL isDir;

        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )

        {

            NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPatherror:nil];

            size+= fileAttributeDic.fileSize;

        }

        else

        {

            [self fileSizeForDir:fullPath];

        }

    }

    [fileManager release];

    return size;

    

}

http://blog.sina.com.cn/s/blog_60b45f230100yd7n.html

0 0