File Memory size calculation, memory size Calculation
/**
* Calculate the size of all files in a file.
Note: to calculate the size of all files in a folder, you must first obtain all files, then obtain the size of all files, and then add them together.
*/
NSFileManager * manager = [NSFileManager defamanager manager];
// Extract the file that requires Calculation
NSArray * arr = [manager subpathsAtPath: @ "/Users/zhangyunjiang/Desktop"];
NSMutableString * strM = [NSMutableString string];
NSMutableString * strM1 = [NSMutableString string];
For (int I = 0; I <arr. count; I ++ ){
// Add all file names to the string
[StrM appendFormat: @ "// Users/zhangyunjiang/Desktop/% @", arr [I];
}
// Convert the string with the added file name into an array
NSArray * arr1 = [strM componentsSeparatedByString: @ "//"];
For (int j = 0; j <arr. count; j ++ ){
// Extract the attribute content of each file name from the array
NSDictionary * dic = [manager attributesOfItemAtPath :( arr1 [j]) error: nil];
// Find the memory size occupied by files in the content
NSString * str3 = dic [NSFileSize];
[StrM1 appendFormat: @ "-% @", str3];
}
// Calculate
NSArray * arr2 = [strM1 componentsSeparatedByString: @ "-"];
Double sum = 0;
For (int I = 0; I <arr2.count; I ++ ){
NSString * str3 = [arr2 objectAtIndex: I];
If (str3 = nil ){
}
Else
{
Sum + = [str3 intValue];
}
}
// Convert the space into a GM
Double newsum = sum/1024/1024/1024;
NSLog (@ "% f", sum );
NSLog (@ "% f", newsum );
}
Return 0;