Directory Size

Andrea Fontana nospam at example.com
Wed Dec 6 09:16:56 UTC 2017


On Tuesday, 5 December 2017 at 17:21:29 UTC, Vino wrote:
> Hi All,
>
>  Is there any better ways to get the size of folders , The 
> below code perfectly works , but i need return type as 
> Array!(Tuple!(string, string)) rather then using the 
> "Result.insertBack(d); 
> Result.insertBack(to!string(SdFiles[].sum))" as per the below 
> example.
>
> E.g:
> Array!(Tuple!(string, string)) Result;
> Result = (d, to!string(SdFiles[].sum));
>
> Program:
> import std.algorithm: filter, map, sum, uniq;
> import std.container.array;
> import std.file: dirEntries, SpanMode, isDir, isFile;
> import std.stdio: writeln;
> import std.typecons: tuple, Tuple;
> import std.conv: to;
> /******************************************/
> /* Sub Function : Size of Dir List        */
> /******************************************/
> auto mSize () {
> 	string FFs = "C:\\Temp\\BACKUP";
> 	Array!string Result;
> 	auto dFiles = Array!string ((dirEntries(FFs, 
> SpanMode.shallow).filter!(a => a.isDir)).map!(a => a.name));
> 	foreach (d; dFiles[]) 	{
> 		auto SdFiles = Array!ulong((dirEntries(d, 
> SpanMode.depth).filter!(a => a.isFile)).map!(a => a.size));
> 		if (SdFiles[].sum / 1024 / 1024  > 30) { 
> Result.insertBack(d); 
> Result.insertBack(to!string(SdFiles[].sum)); }
> 								}
> 		return Result;
> }
>
> void main() {
> writeln(mSize[]);
> }
>
> From,
> Vino.B

Something like:

auto mSize () {
	string FFs = "C:\\Temp\\BACKUP";
	
    return dirEntries(FFs, SpanMode.shallow)
    .filter!(a => a.isDir)
    .map!(a => tuple(a.name, 
a.dirEntries(SpanMode.depth).filter!(a=>a.isFile).map!(a => 
a.size).sum))
    .filter!(a => a[1] > 1024*1024*30)
    .map!(a => tuple(a[0], a[1].to!string))
    .array;
}

?


More information about the Digitalmars-d-learn mailing list