Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
563 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 59

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 59

Accepted Solution

by:
Julian Hansen earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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