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
539 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 57

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 57

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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!
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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