-1

I've asked a very similar question before here. But that was about getting directories, and this is about files. And these codes are a bit different from each other. Ever since I've been trying to convert this to make it look like the answer on my old question, I haven't been able to make it work.

string[] files = Directory.GetFiles(ScanPath, "*.*", System.IO.SearchOption.AllDirectories);
DateTime From = DateTime.Now.AddHours(-24);
DateTime To = DateTime.Now;

foreach (string name in files)
{
   FileInfo file = new FileInfo(name);
   string fullname = file.FullName;

   if (file.LastWriteTime >= From & file.LastWriteTime <= To && file.Length >= ScanSize)
      Console.WriteLine(file.FullName + " ; " + "last changed at  " + " ; " + file.LastWriteTime.ToString());
}

I've been getting the same errors as I explained in the other question. Because I don't know where to put the code of the foreach in a recursion. Since it's not an enumeration but a Directory.GetFiles().

The error occurs with:

Directory.GetFiles(ScanPath, "*", SearchOption.AllDirectories);

because this gets all the files of the directories at once. But if I remove it, it only gets the files in the given path, without any of the files in the subdirectories. So I was told to apply recursion.

I am the administrator of the system and I plan to run this on the entire data drive. D:\

I'm hoping anyone here knows a good example.

Community
  • 1
  • 1

2 Answers2

3

Your app could not have access rights to some folders, for others you can use the following code:

void DiscoverDirs(string where, List<string> files, Func<FileInfo, bool> filter)
{
    try
    {
        var di = new DirectoryInfo(where);
        files.AddRange(di.EnumerateFiles().Where(filter).Select(x => x.FullName));

        foreach (var dir in Directory.GetDirectories(where))
        {
            DiscoverDirs(dir, files, filter);
        }
    }
    catch
    {
        // no access fo this dir, ignore
    }
}

Usage:

DateTime From = DateTime.Now.AddHours(-24);
DateTime To = DateTime.Now;
var ScanSize = 5*1024*1024;

var list = new List<string>();
DiscoverDirs(@"C:\", list, 
    file => file.LastWriteTime >= From & file.LastWriteTime <= To && file.Length >= ScanSize);

foreach (string name in list)
{
    FileInfo file = new FileInfo(name);
    string fullname = file.FullName;

    Console.WriteLine(file.FullName + " ; " + "last changed at  " + " ; " + file.LastWriteTime.ToString());
}
Tony
  • 7,345
  • 3
  • 26
  • 34
  • Just like I said in my question, I was hoping to make it look something like [this](http://stackoverflow.com/a/20545860/3095385). – user3095385 Dec 17 '13 at 10:00
  • I've got the error: "No overload for method 'DiscoverDirs' takes 2 arguments" at `DiscoverDirs(dir, files);` at the `foreach` in the `void DiscoverDirs`. Did you have this as well? – user3095385 Dec 17 '13 at 12:25
  • @user3095385 Sorry, I fixed it. – Tony Dec 17 '13 at 12:41
  • It seems to be working. But the output is wrong. It shows far too less files that are (for example if I want files more or equal then 1 mb) in a directory. I have 8 photoshop files of more then 50 mb, and the program only shows 1. The code seems to be correct though. – user3095385 Dec 17 '13 at 13:22
  • @user3095385 Maybe these files was filtered by this clause `file.LastWriteTime >= From & file.LastWriteTime <= To`? You want to find files, which were modified within last 24 hours. – Tony Dec 17 '13 at 13:26
  • Oh yeah, sorry. I'm used to scanning directories, files are new to me. Thanks a lot for your help and time :) – user3095385 Dec 17 '13 at 13:34
1

You might be getting "UnauthorizedAccessException" while accessing the some of the system directories.List of the directory causing the problems are directories which are actually just redirection to other directory. May be you can tried out the following code if it helps-

try
{
    foreach (String file in Directory.GetFiles(directoryName, pattern, SearchOption.TopDirectoryOnly))
    {
       // do stuff
    }
catch (UnauthorizedAccessException uae)
{
           //handle
}
catch (Exception e) 
{ 
           //handle
}

Alternative:

string[] directories = Directory.GetDirectories(ScanPath);

    foreach (string directory in directories)
    {
        string[] filesinCurrentDirectory = Directory.GetFiles(directory, "*.*", System.IO.SearchOption.AllDirectories);
        foreach (string file in filesinCurrentDirectory)
        {
            MessageBox.Show(file);
        }

    }
Ramashankar
  • 1,598
  • 10
  • 14
  • That's my current format, but I need to recursivly search every accessible file in every directory/subdirectory of a given directory. – user3095385 Dec 17 '13 at 09:50
  • @user3095385: Could you see the alternative option? So basically, when I do Directory.GetDirectories() I am getting all my directories. then i can call GetFiles() method to get the list of files under the current directory. – Ramashankar Dec 17 '13 at 10:04
  • can you please read my question? Errors occur when I start scanning at the top layers of my hard drive. I'm talking about the hidden files which even the administrator cannot access. So `SearchOption.AllDirectories` will not work. – user3095385 Dec 17 '13 at 10:09