Hi everybody! My program takes argument. This argument is a directory, and program will find all files with the same content, group that files and print on screen. With small amount of files, program works good and fast, but, for example, i take my flash-card for searching and compearing(or more dramatic - the C:\ directory), program gradually increase RAM using, until to 500Mb, and write this exception. I post my code, and want to listen the critic and help.
Here the comparing function:
static void CompareFilesRec(List<string> array)
{
List<string> Trash = new List<string>();
List<string> Output = new List<string>();
List<KeyValuePair<long, string>> yeah = new List<KeyValuePair<long, string>>();
List<string> outp = new List<string>();
for (int j = 0; j <= array.Count - 1; j++)
{
FileInfo fii = new FileInfo(array[j]);
yeah.Add(new KeyValuePair<long, string>(fii.Length, array[j]));
}
foreach (var el in yeah)
{
if (!Output.Contains(el.Value) && !Trash.Contains(el.Value))
{
foreach (var ele in yeah)
{
if (el.Key == ele.Key && !Output.Contains(ele.Value))
{
Output.Add(ele.Value);
}
else
{
if (!Trash.Contains(ele.Value))
{
Trash.Add(ele.Value);
}
}
}
}
}
foreach (string f in Output)
{
int valueOne = 0, valueTwo = 0;
StreamReader objReader = new StreamReader(f);
valueOne = objReader.ReadToEnd().GetHashCode();
StreamReader objReader1 = new StreamReader(Output[0]);
valueTwo = objReader1.ReadToEnd().GetHashCode();
if (f != Output[0] && valueOne == valueTwo)
{
outp.Add(f);
}
if (valueOne != valueTwo)
{
Trash.Add(f);
}
}
outp.Add(Output[0]);
if (outp.Count > 1)
{
foreach (string fi in outp)
{
Console.WriteLine(fi);
}
outp.Clear();
Console.WriteLine();
}
if (Trash.Count >= 1)
CompareFilesRec(Trash);
Output.Clear();
Trash.Clear();
outp.Clear();
}
Code:
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApplication3
{
class Comparer
{
static void Main(string[] args)
{
if (args.Length != 0)
{
if (args.Length == 1)
{
List<string> ListOfFiles = new List<string>();
ListOfFiles.AddRange(LookIn(args[0]));
Console.WriteLine("Found {0} files", ListOfFiles.Count);
if(ListOfFiles.Count > 0)
CompareFilesRec(ListOfFiles);
}
else Console.WriteLine("So many arguments... o_O");
}
else Console.WriteLine("Write some stuff here!");
}
static bool CheckFileLessThan2Gb(string file)
{
FileInfo someFileInfo = new FileInfo(file);
if (someFileInfo.Length <= 2147483648)
return true;
else return false;
}
static List<string> LookIn(string path)
{
List<string> files = new List<string>();
List<string> dirs = new List<string>();
try
{
files.AddRange(Directory.GetFiles(path));
dirs.AddRange(Directory.GetDirectories(path));
for (int i = 0; i <= files.Count; i++)
{
if (!CheckFileLessThan2Gb(files[i]))
files.RemoveAt(i);
try
{
string FileValue = null;
StreamReader Value = new StreamReader(files[i]);
FileValue = Value.ReadToEnd();
if (FileValue.Length == 0)
files.RemoveAt(i);
Value.Dispose();
Value.Close();
}
catch (System.IO.IOException) { }
}
}
catch (UnauthorizedAccessException) { }
catch (DirectoryNotFoundException) { }
catch (ArgumentOutOfRangeException) { }
foreach (string dir in dirs)
{
files.AddRange(LookIn(dir));
}
return files;
}
static void CompareFilesRec(List<string> array)
{
List<string> Trash = new List<string>();
List<string> Output = new List<string>();
List<KeyValuePair<long, string>> yeah = new List<KeyValuePair<long, string>>();
List<string> outp = new List<string>();
for (int j = 0; j <= array.Count - 1; j++)
{
FileInfo fii = new FileInfo(array[j]);
yeah.Add(new KeyValuePair<long, string>(fii.Length, array[j]));
}
foreach (var el in yeah)
{
if (!Output.Contains(el.Value) && !Trash.Contains(el.Value))
{
foreach (var ele in yeah)
{
if (el.Key == ele.Key && !Output.Contains(ele.Value))
{
Output.Add(ele.Value);
}
else
{
if (!Trash.Contains(ele.Value))
{
Trash.Add(ele.Value);
}
}
}
}
}
foreach (string f in Output)
{
int valueOne = 0, valueTwo = 0;
StreamReader objReader = new StreamReader(f);
valueOne = objReader.ReadToEnd().GetHashCode();
StreamReader objReader1 = new StreamReader(Output[0]);
valueTwo = objReader1.ReadToEnd().GetHashCode();
if (f != Output[0] && valueOne == valueTwo)
{
outp.Add(f);
}
if (valueOne != valueTwo)
{
Trash.Add(f);
}
}
outp.Add(Output[0]);
if (outp.Count > 1)
{
foreach (string fi in outp)
{
Console.WriteLine(fi);
}
outp.Clear();
Console.WriteLine();
}
if (Trash.Count >= 1)
CompareFilesRec(Trash);
Output.Clear();
Trash.Clear();
outp.Clear();
}
}
}