Solved

List of all the file within a directory and subdirectory that was modified within a date range and have a count on each folder.

Posted on 2013-06-25
3
533 Views
Last Modified: 2013-07-23
I'm trying to have the following output on the console.writeline can someone help?

C:
Count of file
file.CreationTimeUtc.ToString("MM/dd/yyyy hh:mm:ss"), file.DirectoryName, file.Name
C:/subfolder name
Count of file
file.CreationTimeUtc.ToString("MM/dd/yyyy hh:mm:ss"), file.DirectoryName, file.Name
C:/subfolder name/subfolder name
Count of file
file.CreationTimeUtc.ToString("MM/dd/yyyy hh:mm:ss"), file.DirectoryName, file.Name

Here is my original code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        
        DirectoryInfo DirInfo = new DirectoryInfo(@"W:\");
           
DateTime dt1 = new DateTime(2013, 01, 1);
DateTime dt2 = DateTime.Now;

// LINQ query for files between 15-July 2009 and 15-April 2010.
var files = from file in DirInfo.EnumerateFiles()
where file.CreationTimeUtc > dt1 &
file.CreationTimeUtc < dt2                       
select file;

// Show results.
foreach (var file in files)
  {
    Console.WriteLine("{0} {1}{2}", file.CreationTimeUtc.ToString("MM/dd/yyyy hh:mm:ss"), file.DirectoryName, file.Name);
  }
Console.ReadLine();
}
}
        
        }

Open in new window

0
Comment
Question by:yguyon28
  • 2
3 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39274901
Your function needs to be recursive.

For each folder you find you need to call the same function on that folder something like this
static void searchSubFolder(String folderName)
{
    DirectoryInfo DirInfo = new DirectoryInfo(folderName);
    DateTime dt1 = new DateTime(2013, 01, 1);
    DateTime dt2 = DateTime.Now;

    // LINQ query for files between 15-July 2009 and 15-April 2010.
    var files = from file in DirInfo.EnumerateFiles()
    where file.CreationTimeUtc > dt1 &
    file.CreationTimeUtc < dt2                       
    select file;

    // Show results.
    foreach (var file in files)
    {
        Console.WriteLine("{0} {1}{2}", file.CreationTimeUtc.ToString("MM/dd/yyyy hh:mm:ss"), file.DirectoryName, file.Name);
        searchSubFolder(folderName + @"\" + file.name);
  }
}

Open in new window

And then call this with the root of the folder you want to search from
searchSubFolder(@"W:\");

Open in new window

;
0
 

Author Comment

by:yguyon28
ID: 39274962
close but I was looking that it scan the entire c: drive and return all folder and subfolders within associated files name that was modified within a date range.
0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39275232
Does this do what you need

static void Main(string[] args)
{
  searchSubFolder(@"g:\projects");
}

static void searchSubFolder(String folderName)
{
  DirectoryInfo DirInfo = new DirectoryInfo(folderName);

  DateTime dt1 = new DateTime(2013, 01, 1);
  DateTime dt2 = DateTime.Now;

  // LINQ query for files between 15-July 2009 and 15-April 2010.
  var files = from file in DirInfo.EnumerateFiles()
    where file.CreationTimeUtc > dt1 &
    file.CreationTimeUtc < dt2
    select file;

  // Show results.
  foreach (var file in files)
  {
    Console.WriteLine("{0} {1}\\{2}", file.CreationTimeUtc.ToString("MM/dd/yyyy hh:mm:ss"), file.DirectoryName, file.Name);
  }

  foreach(var dir in DirInfo.EnumerateDirectories())
  {
    searchSubFolder(folderName + @"\" + dir.Name);
  }
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 38
Get distinct values from excel or comm seperated file 4 32
Linq asp.net mvc 13 17
Upgrade code from VS 2010 to VS 2015 7 20
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question