Skip to content

Instantly share code, notes, and snippets.

@janhebnes
Last active December 16, 2015 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janhebnes/97d701658d762ad7f1f0 to your computer and use it in GitHub Desktop.
Save janhebnes/97d701658d762ad7f1f0 to your computer and use it in GitHub Desktop.
RecursiveFileSearch - compile and dump in the root of a NAS disk and run once in a while to get a full directory tree in a text file in the root allowing for quicker searches by just opening the text file - used for very large NAS shares.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RecursiveFileSearch
{
class Program
{
static StringBuilder log = new StringBuilder();
static StringBuilder masterLog = new StringBuilder();
static void Main(string[] args)
{
var path = Directory.GetCurrentDirectory();
if (args.Count() == 1)
{
// Start with drives if you have to search the entire computer.
path = args[0];
}
if (!Directory.Exists(path))
{
Console.Write("Could not find path: {0}", path);
return;
}
var root = new DirectoryInfo(path);
var subDirs = root.GetDirectories();
var dumpToRoot = true;
foreach (System.IO.DirectoryInfo dirInfo in subDirs.OrderBy(d=>d.Name))
{
if (dirInfo.Name.StartsWith(".")
|| dirInfo.Name.StartsWith("#"))
{
continue;
}
WalkDirectoryTree(dirInfo);
if (dirInfo.Name.IndexOf(' ') == 3)
{
// if dirInfo name has client initials dump files here.
DumpLogFile(dirInfo);
dumpToRoot = false;
}
}
if (dumpToRoot)
{
DumpLogFile(root);
}
else
{
if (masterLog.Length > 0)
{
var masterpath = Path.Combine(root.FullName, "filescan.txt");
using (var stream = new StreamWriter(masterpath, false))
{
stream.Write(masterLog.ToString());
stream.Close();
}
Console.WriteLine(masterpath);
}
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key");
Console.ReadKey();
}
private static void DumpLogFile(DirectoryInfo dirInfo)
{
if (log.Length == 0)
{
return;
}
var path = Path.Combine(dirInfo.FullName, "filescan.txt");
using (var stream = new StreamWriter(path, false))
{
stream.Write(log.ToString());
stream.Close();
}
masterLog.Append(log);
log = new StringBuilder();
Console.WriteLine(path);
}
static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
try
{
files = root.GetFiles("*.*");
}
// This is thrown if even one of the files requires permissions greater
// than the application provides.
catch (UnauthorizedAccessException e)
{
// This code just writes out the message and continues to recurse.
// You may decide to do something different here. For example, you
// can try to elevate your privileges and access the file again.
log.AppendLine(e.Message);
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
foreach (System.IO.FileInfo fi in files.OrderBy(d => d.Name))
{
if (fi.Name.StartsWith(".")
|| fi.Name.StartsWith("#")
|| fi.Name.StartsWith("XXX")
|| fi.Name.StartsWith("~"))
{
continue;
}
// In this example, we only access the existing FileInfo object. If we
// want to open, delete or modify the file, then
// a try-catch block is required here to handle the case
// where the file has been deleted since the call to TraverseTree().
try
{
log.AppendLine(string.Format("{0} ({1})", fi.FullName, fi.LastWriteTime.ToString("g")));
}
catch (System.IO.PathTooLongException exception)
{
log.AppendLine(
string.Format("#error could not scan file root because of PathTooLongException {0} ", root));
Console.WriteLine("PathTooLongException in {0}", root);
}
catch (ArgumentOutOfRangeException exception)
{
log.AppendLine(
string.Format("{0} #invalid filedate", fi.FullName));
Console.WriteLine("ArgumentOutOfRangeException on FileDate {0}", fi.FullName);
}
}
// Now find all the subdirectories under this directory.
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs.OrderBy(d => d.Name))
{
if (dirInfo.Name.StartsWith(".")
|| dirInfo.Name.StartsWith("#"))
{
continue;
}
try
{
// Resursive call for each subdirectory.
WalkDirectoryTree(dirInfo);
}
catch (System.IO.PathTooLongException exception)
{
log.AppendLine(string.Format("#error could not scan folder {0} because of PathTooLongException ", dirInfo.Name));
Console.WriteLine("PathTooLongException in {0}", dirInfo.Name);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment