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
529 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 54

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 54

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 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