Container Array

Vino.B via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 7 10:12:14 PDT 2017


On Thursday, 7 September 2017 at 15:07:56 UTC, Vino.B wrote:
> On Thursday, 7 September 2017 at 14:26:08 UTC, Ali Çehreli 
> wrote:
>> On 09/07/2017 03:56 AM, Vino.B wrote:
>>
>>> writeln(coCleanFiles);
>>
>> Access the elements by taking a slice of the container:
>>
>>     writeln(coCleanFiles[]);
>>
>> Ali
>
> Hi Ali,
>
>  Thank you very much, was ablee to resolve this issue and now 
> facing a new issue as the below code is not working as 
> expected. The below code has to list the folders and their 
> size's.
>
> import std.algorithm: filter, map, fold;
> import std.container;
> import std.file: SpanMode, dirEntries, isDir;
> import std.stdio: File, writefln, writeln;
> import std.typecons: tuple, Tuple;
> import std.parallelism: parallel;
>
> Array!(Tuple!(string, ulong)) coSizeDirList () {
> 	string FFs = "C:\\Temp\\TEST1\\BACKUP";
> 	int SizeDir = 10;
> 	ulong subdirTotal;
> 	ulong subdirTotalGB;
> 	auto dFiles = Array!(Tuple!(string)) (dirEntries(FFs, 
> SpanMode.shallow).filter!(a => a.isDir).map!(a => 
> tuple(a.name)));
>     foreach (d; dFiles) {
> 				auto SdFiles = Array!(Tuple!(ulong)) (dirEntries(d[0], 
> SpanMode.depth).map!(a => tuple(a.size)));
> 				foreach(f; parallel(SdFiles, 1)) { subdirTotal += 
> f.fold!((a, b) => a + b); }
> 					subdirTotalGB = (subdirTotal/1024/1024);
> 					if (subdirTotalGB > SizeDir) { auto Subdata = 
> Array!(Tuple!(string, ulong))(dFiles ~ subdirTotalGB); }
> 					 subdirTotal = 0;
> 		    }
> 			return Subdata;
> }
>
> void main () {
> writeln (coSizeDirList[]);
> }

Hi,

  Few updates,

  If i change the function to main i am able to print the required 
output, but if i change the main to function and call this 
function from another main then i am not able to return the 
result from the function.

Updated Code:
import std.algorithm: filter, map, fold;
import std.container;
import std.file: SpanMode, dirEntries, isDir;
import std.stdio: File, writefln, writeln;
import std.typecons: tuple, Tuple;
import std.parallelism: parallel;
import std.conv;
Array!(Tuple!(string, ulong)) coSizeDirList () {
//void main () {
	string FFs = "C:\\Temp\\sapnas2\\BACKUP";
	int SizeDir = 1;
	ulong subdirTotal;
	ulong subdirTotalGB;
	Array!(Tuple!(ulong)) Subdata;
	auto dFiles = Array!(Tuple!(string)) (dirEntries(FFs, 
SpanMode.shallow).filter!(a => a.isDir).map!(a => tuple(a.name)));
     foreach (d; dFiles[]) {
				auto SdFiles = Array!(Tuple!(ulong)) (dirEntries(d[0], 
SpanMode.depth).map!(a => tuple(a.size)));
				foreach(f; SdFiles[]) { subdirTotal += f.fold!((a, b) => a + 
b); }
					subdirTotalGB = (subdirTotal/1024/1024);
					if (subdirTotalGB > SizeDir) { Subdata ~= subdirTotalGB; }
					subdirTotal = 0;
		    }
			//writeln(dFiles);
			//writeln(Subdata);
			return dFiles[];
			return Subdata[];
}

void main () {
writeln (coSizeDirList[]);
}

From,
Vino.B



More information about the Digitalmars-d-learn mailing list