Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question is not very clear, but looking at your code, and what you state you want to achieve, I would recommend that you get rid of the loop that takes you through the date folders. Just use the "AllDirectories" option, under each of the top-level folders. It is recursive, so it will go down through as many levels as there are. </p> <pre><code>TimeSpan BeginningTime = DateTime.Now.TimeOfDay; DateTime BeginningDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); DateTime EndingDate = DateTime.Now; string[] FoldersToLookAt = { @"e:\", @"e:\Kodak Images\", @"e:\images\", @"e:\AFSImageMerge\" }; foreach (string FolderPath in FoldersToLookAt) { FilesToLookThrough = new DirectoryInfo(FolderPath); if (FilesToLookThrough.Exists) { foreach (var MyFile in FilesToLookThrough.EnumerateFiles("*.dat", SearchOption.AllDirectories)) { if (MyFile.LastAccessTime &gt;= BeginningDate) { Console.WriteLine(MyFile.FullName); } } } } </code></pre> <p>EDIT: The other answer makes a good point, since you're going through "e:\", you probably don't need to go through the other "FoldersToLookAt", since they will all be searched anyway. What you might end up with here is multiple listings of the same file. If you take those out, it will run quite a bit faster.</p> <p>You see, your code was pretty close in the first place. Using this approach you cut out a whole loop, and the "AllDirectories" search option will make sure that you look through all the sub-folders recursively. You are also protected against your organisation deciding not to store stuff in folders named by date etc, and now the runtime of your program is proportional only to the number of files.</p> <p>Now, for extra credit, another big performance improvement can be made by not using Console.WriteLine for each item. A faster way would be to use a StringBuilder, and then spit the results out at the end.</p> <pre><code>// At the top of your program StringBuilder sb = new StringBuilder(); // BeginningTime, BeginningDate, etc... // Before the first loop Console.WriteLine("Working..."); // Inside the very inner if, where your Console.WriteLine was sb.AppendLine(MyFile.FullName); // After the end of the outer loop Console.WriteLine(sb.ToString()); </code></pre> <p>Why does this make it better? Writing to the console is notoriously slow, it actually involves sending Windows into kernel mode and back, it's really, really slow. Doing it once, event with a much larger chunk of text, is much, much quicker, than doing it lots. Now, why use a StringBuilder instead of just doing a good old:</p> <pre><code>string output; for(...) { output += filename + Environment.NewLine; } </code></pre> <p>In C# adding two strings to eachother creates a new string. Doing this over and over again is also slow, especially as the new string gets larger. StringBuilder just maintains a list of all the strings, and creates a new buffer and copies them all in, once, when you call ToString().</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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