www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Different Output after each execution

reply Vino.B <vino.bheeman hotmail.com> writes:
Hi All,

  I have written a small program to just list the directories, but 
when i run the program each time i am getting different output, 
hence request you help, below is the code

Program:
import std.file: dirEntries, isFile, SpanMode, remove;
import std.stdio: writefln;
import std.algorithm: filter;
import std.parallelism: parallel;
import std.array: array;
import std.datetime;

auto AgedDirlst = [ "C:\\Temp\\TEAM", "C:\\Temp\\PROD_TEAM", 
"C:\\Temp\\BACKUP", "C:\\Temp\\EXPORT", 
"C:\\Temp\\sapnas3\\BACKUP", "C:\\Temp\\EXPORT"];


void AgedDir (string[] Dirlist)
{
  for (auto i = 0; i < Dirlist.length; ++i)
      {
       auto dFiles = dirEntries(Dirlist[i], 
SpanMode.shallow).filter!(a => a.isDir);
	  foreach (d; parallel(dFiles , 1))
	          {
			    writefln("%-63s %.20s", d, d.timeCreated().toSimpleString);
			  }
	 }
}

void main ()
{
  AgedDir(AgedDirlst);
}

If i replace the line(.isDir to .isFile) "auto dFiles = 
dirEntries(Dirlist[i], SpanMode.shallow).filter!(a => a.isDir)" 
to auto dFiles = dirEntries(Dirlist[i], 
SpanMode.shallow).filter!(a => a.isFile), then is it working 
perfectly.

From,
Vino.B
Output
C:\Users\Admin\Desktop\Script\D>rdmd AgedDir.d
C:\Temp\TEAM\DIR1                                       
2017-Aug-16 18:49:21
C:\Temp\TEAM\DIR1                                       
2017-Aug-16 18:49:21
C:\Temp\TEAM\DND1                                       
2017-Jun-30 21:02:09
C:\Temp\TEAM\DIR2                                       
2017-Jun-29 23:21:36
C:\Temp\TEAM\DND5                                       
2017-Jun-30 23:49:24
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\dir2                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\DND1                                  
2017-Jun-30 21:08:32

C:\Users\Admin\Desktop\Script\D>rdmd AgedDir.d
C:\Temp\TEAM\DIR1                                       
2017-Aug-16 18:49:21
C:\Temp\TEAM\DIR2                                       
2017-Jun-29 23:21:36
C:\Temp\TEAM\DND1                                       
2017-Jun-30 21:02:09
C:\Temp\TEAM\DND5                                       
2017-Jun-30 23:49:24
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\dir2                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\DND1                                  
2017-Jun-30 21:08:32

C:\Users\Admin\Desktop\Script\D>rdmd AgedDir.d
C:\Temp\TEAM\DND1                                       
2017-Jun-30 21:02:09
C:\Temp\TEAM\DIR1                                       
2017-Aug-16 18:49:21
C:\Temp\TEAM\DIR2                                       
2017-Jun-29 23:21:36
C:\Temp\TEAM\DND5                                       
2017-Jun-30 23:49:24
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05_TEAM\dir2
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05_TEAM\dir2
                   2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\DND1                                  
2017-Jun-30 21:08:32

C:\Users\Admin\Desktop\Script\D>rdmd AgedDir.d
C:\Temp\TEAM\DIR1                                       
2017-Aug-16 18:49:21
C:\Temp\TEAM\DIR1                                       
2017-Aug-16 18:49:21
C:\Temp\TEAM\DND1                                       
2017-Jun-30 21:02:09
C:\Temp\TEAM\DIR2                   C:\Temp\TEAM\DND5             
                           2017-Jun-30 23:49:24
C:\Temp\TEAM\DIR2                   C:\Temp\TEAM\DND5             
                           2017-Jun-30 23:49:24
                     2017-Jun-29 23:21:36
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\dir1                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\dir2                                  
2017-Jun-30 05:38:05
C:\Temp\PROD_TEAM\DND1                                  
2017-Jun-30 21:08:32
Aug 18 2017
parent reply ikod <geller.garry gmail.com> writes:
On Friday, 18 August 2017 at 08:00:26 UTC, Vino.B wrote:
 Hi All,

  I have written a small program to just list the directories, 
 but when i run the program each time i am getting different 
 output, hence request you help, below is the code

 [...]
Do you expect some strict execution order when you run 'parallel' foreach?
Aug 18 2017
parent reply Vino <vino.bheeman hotmail.com> writes:
On Friday, 18 August 2017 at 08:34:39 UTC, ikod wrote:
 On Friday, 18 August 2017 at 08:00:26 UTC, Vino.B wrote:
 Hi All,

  I have written a small program to just list the directories, 
 but when i run the program each time i am getting different 
 output, hence request you help, below is the code

 [...]
Do you expect some strict execution order when you run 'parallel' foreach?
Yes, the order of execution should be the same as the order of the directory provided to scan.
Aug 18 2017
parent reply Moritz Maxeiner <moritz ucworks.org> writes:
On Friday, 18 August 2017 at 10:06:04 UTC, Vino wrote:
 On Friday, 18 August 2017 at 08:34:39 UTC, ikod wrote:
 On Friday, 18 August 2017 at 08:00:26 UTC, Vino.B wrote:
 Hi All,

  I have written a small program to just list the directories, 
 but when i run the program each time i am getting different 
 output, hence request you help, below is the code

 [...]
Do you expect some strict execution order when you run 'parallel' foreach?
Yes, the order of execution should be the same as the order of the directory provided to scan.
Then you cannot parallelize the work[1], use: --- auto dFiles = dirEntries(Dirlist[i], SpanMode.shallow).filter!(a => a.isDir); foreach (d; dFiles) { writefln("%-63s %.20s", d, d.timeCreated().toSimpleString); } --- [1] You cannot parallelize computations that depend on each other, which you make yours do by requiring a specific order of execution.
Aug 18 2017
parent reply Moritz Maxeiner <moritz ucworks.org> writes:
On Friday, 18 August 2017 at 10:50:28 UTC, Moritz Maxeiner wrote:
 On Friday, 18 August 2017 at 10:06:04 UTC, Vino wrote:
 On Friday, 18 August 2017 at 08:34:39 UTC, ikod wrote:
 On Friday, 18 August 2017 at 08:00:26 UTC, Vino.B wrote:
 Hi All,

  I have written a small program to just list the 
 directories, but when i run the program each time i am 
 getting different output, hence request you help, below is 
 the code

 [...]
Do you expect some strict execution order when you run 'parallel' foreach?
Yes, the order of execution should be the same as the order of the directory provided to scan.
Then you cannot parallelize the work[1], use: --- auto dFiles = dirEntries(Dirlist[i], SpanMode.shallow).filter!(a => a.isDir); foreach (d; dFiles) { writefln("%-63s %.20s", d, d.timeCreated().toSimpleString); } --- [1] You cannot parallelize computations that depend on each other, which you make yours do by requiring a specific order of execution.
Small correction: You *could* parallelize the conversion to string `d.timeCreated().toSimpleString`, but then you'd need to merge the resulting sets of strings generated in each work unit to regain the original order.
Aug 18 2017
parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Friday, 18 August 2017 at 11:24:24 UTC, Moritz Maxeiner wrote:
 On Friday, 18 August 2017 at 10:50:28 UTC, Moritz Maxeiner 
 wrote:
 On Friday, 18 August 2017 at 10:06:04 UTC, Vino wrote:
 On Friday, 18 August 2017 at 08:34:39 UTC, ikod wrote:
 On Friday, 18 August 2017 at 08:00:26 UTC, Vino.B wrote:
 Hi All,

  I have written a small program to just list the 
 directories, but when i run the program each time i am 
 getting different output, hence request you help, below is 
 the code

 [...]
Do you expect some strict execution order when you run 'parallel' foreach?
Yes, the order of execution should be the same as the order of the directory provided to scan.
Then you cannot parallelize the work[1], use: --- auto dFiles = dirEntries(Dirlist[i], SpanMode.shallow).filter!(a => a.isDir); foreach (d; dFiles) { writefln("%-63s %.20s", d, d.timeCreated().toSimpleString); } --- [1] You cannot parallelize computations that depend on each other, which you make yours do by requiring a specific order of execution.
Small correction: You *could* parallelize the conversion to string `d.timeCreated().toSimpleString`, but then you'd need to merge the resulting sets of strings generated in each work unit to regain the original order.
Hi, Thank you very much, it worked and need one more help, with the below line i am able to list all directories which contains the pattern *DND*, now i need the revers, list all the directories expect those containing the pattern *DND*. dirEntries(i, SpanMode.shallow).filter!(a => a.isDir).filter!(a => globMatch(a.baseName, "*DND*"))
Aug 18 2017
parent reply Moritz Maxeiner <moritz ucworks.org> writes:
On Friday, 18 August 2017 at 15:46:13 UTC, Vino.B wrote:
 On Friday, 18 August 2017 at 11:24:24 UTC, Moritz Maxeiner 
 wrote:
 On Friday, 18 August 2017 at 10:50:28 UTC, Moritz Maxeiner 
 wrote:
 On Friday, 18 August 2017 at 10:06:04 UTC, Vino wrote:
 On Friday, 18 August 2017 at 08:34:39 UTC, ikod wrote:
 On Friday, 18 August 2017 at 08:00:26 UTC, Vino.B wrote:
 Hi All,

  I have written a small program to just list the 
 directories, but when i run the program each time i am 
 getting different output, hence request you help, below is 
 the code

 [...]
Do you expect some strict execution order when you run 'parallel' foreach?
Yes, the order of execution should be the same as the order of the directory provided to scan.
Then you cannot parallelize the work[1], use: --- auto dFiles = dirEntries(Dirlist[i], SpanMode.shallow).filter!(a => a.isDir); foreach (d; dFiles) { writefln("%-63s %.20s", d, d.timeCreated().toSimpleString); } --- [1] You cannot parallelize computations that depend on each other, which you make yours do by requiring a specific order of execution.
Small correction: You *could* parallelize the conversion to string `d.timeCreated().toSimpleString`, but then you'd need to merge the resulting sets of strings generated in each work unit to regain the original order.
Hi, Thank you very much, it worked and need one more help, with the below line i am able to list all directories which contains the pattern *DND*, now i need the revers, list all the directories expect those containing the pattern *DND*. dirEntries(i, SpanMode.shallow).filter!(a => a.isDir).filter!(a => globMatch(a.baseName, "*DND*"))
Negating the filtering rule should yield you the inverse set: --- dirEntries(i, SpanMode.shallow).filter!(a => a.isDir).filter!(a => !globMatch(a.baseName, "*DND*")) --- Also I don't see a reason to use two filter invocations here, you can join the conditions to a single filter (same for the unnegated one): --- dirEntries(i, SpanMode.shallow).filter!(a => a.isDir && !globMatch(a.baseName, "*DND*")) ---
Aug 18 2017
parent Vino.B <vino.bheeman hotmail.com> writes:
On Friday, 18 August 2017 at 16:53:46 UTC, Moritz Maxeiner wrote:
 On Friday, 18 August 2017 at 15:46:13 UTC, Vino.B wrote:
 [...]
Negating the filtering rule should yield you the inverse set: --- dirEntries(i, SpanMode.shallow).filter!(a => a.isDir).filter!(a => !globMatch(a.baseName, "*DND*")) --- Also I don't see a reason to use two filter invocations here, you can join the conditions to a single filter (same for the unnegated one): --- dirEntries(i, SpanMode.shallow).filter!(a => a.isDir && !globMatch(a.baseName, "*DND*")) ---
Thank you very much, it resolved the issue and also update my code as advised.
Aug 19 2017