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
523 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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now