You are on page 1of 4

using System;

using System.IO;
using System.Security.Principal;
using System.Runtime.InteropServices;

namespace FileCheck
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class FileCheck1
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("kernel32.dll")]
public static extern bool SetConsoleTitle(String lpConsoleTitle);

const int HowDeepToScan=10;


const string Extensions="*.doc;*.xls;*.pdf";
const string FileLog="C:/filelog.txt";
public class Count
{
public int value;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]

static void Main(string[] args)


{
SetConsoleTitle("FileCheck");
StreamWriter fs_out = File.CreateText(FileLog);
string StartPath = @"C:\";
bool ShowWindow1;
string Flag1;
DateTime CheckDate;
Count ct = new Count();
int Iteration = 1;
ct.value = 0;
ShowWindow1 = true;

try
{
Flag1 = args[1].ToString();
if(Flag1 == "/h")
{
ShowWindow1 = false;
}
}
catch
{
ShowWindow1 = true;
}
finally
{
setConsoleWindowVisibility(ShowWindow1,"FileCheck");
}

if (ShowWindow1) Console.WriteLine("Format FileCheck.exe specify


date:<mm/dd/yyyy> hide window:</h>");
try
{
CheckDate = DateTime.Parse(args[0]);
if (ShowWindow1) Console.WriteLine("Using " +
args[0].ToString() + " for CheckDate.");
}
catch
{
if (ShowWindow1) Console.WriteLine("Invalid Date
Argument.");
if (ShowWindow1) Console.WriteLine("Using " +
DateTime.Now.AddYears(-3) + ".");
CheckDate = DateTime.Now.AddYears(-3);
}

if (ShowWindow1) Console.WriteLine("Searching for " +


Extensions);
if (ShowWindow1) Console.WriteLine("Output to " + FileLog);
fs_out.WriteLine("Started " + DateTime.Now.ToString());
fs_out.WriteLine(@"Computer\User Name: " +
WindowsIdentity.GetCurrent().Name.ToString());
ProcessDir(StartPath, Iteration, fs_out, ct, CheckDate,
ShowWindow1);
fs_out.WriteLine(ct.value + " files found.");
fs_out.WriteLine("Finished " + DateTime.Now.ToString());
fs_out.Close();
}

public static int ProcessDir(string sourceDir, int recursionLvl,


StreamWriter r, Count Index, DateTime dtCheck, bool Visible)
{
string [] m_Ext = Extensions.Split(';');
if (recursionLvl<=HowDeepToScan)
{
foreach(string filter in m_Ext)
{
try
{
string [] fileEntries =
Directory.GetFiles(sourceDir, filter);
// Process the list of files found in the
directory.
foreach(string fileName in fileEntries)
{
if (Visible) Console.Write(".");
if(File.GetLastWriteTime(fileName) <=
dtCheck)
{
r.WriteLine(fileName + "\t" +
File.GetLastWriteTime(fileName));
Index.value = Index.value + 1;
}
}
}
catch
{
r.WriteLine(sourceDir + " access denied.");
}

// Recurse into subdirectories of this directory.


try
{
string [] subdirEntries =
Directory.GetDirectories(sourceDir);
foreach(string subdir in subdirEntries)

// Do not iterate through reparse points


if ((File.GetAttributes(subdir) &
FileAttributes.ReparsePoint) !=
FileAttributes.ReparsePoint)

ProcessDir(subdir,recursionLvl+1,r,Index,dtCheck,Visible);
}
catch
{
r.WriteLine(sourceDir + " access denied.");
}
}
return Index.value;
}
public static void setConsoleWindowVisibility(bool visible, string
title)
{
IntPtr hWnd = FindWindow(null, title);

if (hWnd != IntPtr.Zero)
{
if (!visible)
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
else
//Show window again
ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}
}
}
}

//Sameple Output
//Started 8/7/2008 11:21:13 AM
//Computer\User Name: MYpc\MYusername
//C:\Program Files\Microsoft Visual Studio .NET 2003\PreEmptive
Solutions\Dotfuscator Community Edition\docs\userguide.pdf 8/7/2002 2:58:48 PM
//C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs\Assembly Metadata Unmanaged API.doc 10/31/2002 12:30:30 AM
//C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs\Debug.doc 10/3/2002 8:46:27 PM
//C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs\DebugRef.doc 7/24/2002 11:32:25 PM
//C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs\DeclarativeSecuritySupport.doc 4/7/2002 11:37:16 PM
//C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs\Hosting Interfaces.doc 10/31/2002 12:30:30 AM
//C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs\ILDasmAdvancedOptions.doc 8/21/2001 2:51:24 PM
//C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs\Metadata Unmanaged API.doc 7/25/2002 5:30:24 PM
//<Number of> files found.
//Finished 8/7/2008 11:21:30 AM