Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It sounds like, given a list of directories to search, find all files in certain subdirectories of the directories to be searched that match a particular pattern and have have been touched within the current month. Given that problem statement, something like this should do you:</p> <pre><code>static IEnumerable&lt;FileInfo&gt; FindFiles( IEnumerable&lt;string&gt; directories , string searchPattern ) { DateTime dtNow = DateTime.Now.Date ; // current date DateTime dtFrom = dtNow.AddDays( dtNow.Day - 1 ) ; // compute the first of the month @ start-of-day DateTime dtThru = dtFrom.AddMonths(1).AddTicks(-1) ; // compute the last of the month @ end-of-day string childPattern = dtFrom.ToString( "yyMM*") ; return directories.Select( x =&gt; new DirectoryInfo( x ) ) .Where( x =&gt; x.Exists ) .SelectMany( x =&gt; x.EnumerateDirectories( childPattern , SearchOption.TopDirectoryOnly ) .Where( subDir =&gt; { int dd ; int.TryParse( subDir.Name.Substring(4,2) , out dd ) ; return dd &gt;= dtFrom.Day &amp;&amp; dd &lt;= dtThru.Day ; }) ) .SelectMany( subDir =&gt; subDir.EnumerateFiles( searchPattern , SearchOption.TopDirectoryOnly ) .Where( file =&gt; file.LastAccessTime &gt;= dtFrom &amp;&amp; file.LastAccessTime &lt;= dtThru ) ) ; } </code></pre> <p>An explanation of what this code does:</p> <pre><code>directories.Select( x =&gt; new DirectoryInfo( x ) ) </code></pre> <p>Takes the supplied enumerable list of string directory paths and converts it into an enumerable list of <code>DirectoryInfo</code> objects representing the specified directories</p> <pre><code>.Where( x =&gt; x.Exists ) </code></pre> <p>Excludes any non-existent directories</p> <p>This gives use the set of root directories to be searched.</p> <p>The next clause is a little more complicated. <code>SelectMany()</code> takes an enumerable list of things. Each item in the list is converted into an enumerable list of things (which might or might not be the same type of object as the original. However, each such sublist must be of the same type.)</p> <p>The resulting "list of lists" is then <em>flattened</em> to produce a single enumerable list.</p> <p>With that in mind,</p> <pre><code>.SelectMany( x =&gt; x.EnumerateDirectories( childPattern , SearchOption.TopDirectoryOnly ) .Where( subDir =&gt; { int dd ; int.TryParse( subDir.Name.Substring(4,2) , out dd ) ; return dd &gt;= dtFrom.Day &amp;&amp; dd &lt;= dtThru.Day ; }) ) </code></pre> <p>Converts each root directory into a list of subdirectories, whose name starts with the year and month specified (<code>yyMM*</code>) and whose 4th &amp; 5th characters are the day of the month. That list of lists of subdirectories is then flattened into a single list of subdirectories.</p> <p>The last <code>SelectMany()</code></p> <pre><code>.SelectMany( subDir =&gt; subDir.EnumerateFiles( searchPattern , SearchOption.TopDirectoryOnly ) .Where( file =&gt; file.LastAccessTime &gt;= dtFrom &amp;&amp; file.LastAccessTime &lt;= dtThru ) ) </code></pre> <p>goes over the list of subdirectories resulting from the first <code>SelectMany()</code>, searching each for files whose names match the specified name pattern (<code>*.dat</code> in your example) and whose last access time is within the specified time frame.</p> <p>The resulting list of lists of FileInfo objects is then flattened into a single list of FileInfo objects representing the files in which you're interested.</p> <p>You can then access them directorly, something like</p> <pre><code>string[] searchDirs = { @"e:\" , @"e:\Kodak Images\" , @"e:\images\" , @"e:\AFSImageMerge\" } ; foreach ( FileInfo fi in FindFiles( searchDirs , "*.dat" ) { do_something_with_interesting_file( fi ) ; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload