刪除重復的文件功能
使用方法:
建一個BAT文件,如1.bat,裡面寫入:RemoveDuplicate.exe path1 path2 (或者在命令行下輸入以上內容)
其中path1表示原文件夾,path2表示要檢測和刪除的文件夾
例如文件夾path1中有:1.txt、2.txt、3.txt、4.txt、5.txt
例如文件夾path2中有:4.txt、5.txt、6.txt、7.txt、8.txt
(path1和path2中4.txt、5.txt是同名同大小的文件)
執行 RemoveDuplicate.exe path1 path2
之後:
文件夾path1中有:1.txt、2.txt、3.txt、4.txt、5.txt
文件夾path2中有:6.txt、7.txt、8.txt
其中文件夾path2中的4.txt、5.txt會被刪除。
寫此方法的目的:
本人有兩台開發機和一台家用機,平時很多源代碼和設計文件在各個機器上轉來轉去,復制很多份,最近發現其中一台開發機容量已經爆滿,想著把兩台開發機和家用機上面的所有源代碼和設計文檔做一個去重復的處理(兩台開發機上都有類似網蟲的監控服務,會監控和下載網絡上的很多資源),只保留其中一套,例如開發機A、開發機B、家用機C,以“開發機A”作為基礎,去刪除“開發機B”、“家用機C”上重復的源代碼和各種文檔。
-----------------------------------------------------------
可將本程序放入 “開發機A”,在控制台下執行 RemoveDuplicate.exe pathA ,其中 pathA 表示基礎路徑(以其中的源代碼和各種文檔作為參照),執行之後會生成一個all.conf文件,其中記載“開發機A” pathA路徑下所有文件的信息(名稱、路徑、大小);
例如將RemoveDuplicate.exe放入“開發機A”的D盤符下
控制台輸入命令 cd \d d:\ 切換到D盤符
控制台輸入 RemoveDuplicate.exe d:\ 或者 RemoveDuplicate.exe "d:\"
會在D盤下生成一個all.conf文件
-------------------------------------------------------------
然後將本程序RemoveDuplicate.exe和all.conf文件放入“開發機B”,在控制台下執行 RemoveDuplicate.exe "an exists directory" pathB ,其中 "an exists directory" 表示一個不存在的文件路徑,可以直接寫成" "(空字符串千萬不要省略引號),或者寫成 aaaaaaaaa 等一個不存在的路徑,pathB 表示“開發機B”需要被檢查和刪除的文件夾路徑;
例如將RemoveDuplicate.exe放入“開發機B”的D盤符下
控制台輸入命令 cd \d d:\ 切換到D盤符
控制台輸入 RemoveDuplicate.exe " " d:\ e:\ 或者 RemoveDuplicate.exe " " "d:\" "e:\"
會將“開發機B”上d:\和e:\路徑下與all.conf中相同的文件給刪除。
然後控制台輸入 RemoveDuplicate.exe d:\ 將“開發機B”的D盤符下所有文件都計入all.conf中
然後控制台輸入 RemoveDuplicate.exe e:\ 將“開發機B”的E盤符下所有文件都計入all.conf中
-------------------------------------------------------------
然後將本程序RemoveDuplicate.exe和all.conf文件放入“家用機C”,在控制台下執行 RemoveDuplicate.exe "an exists directory" pathC(其余同上);
例如將RemoveDuplicate.exe放入“家用機C”的D盤符下
控制台輸入命令 cd \d d:\ 切換到D盤符
控制台輸入 RemoveDuplicate.exe " " e:\ 或者 RemoveDuplicate.exe " " "e:\"
會將“家用機C”上e:\路徑下與all.conf中相同的文件給刪除。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace RemoveDuplicate
{
public class MFile
{
public string Name { set; get; }
public string FullName { set; get; }
public long Length { set; get; }
}
class Program
{
static List<MFile> listFiles = new List<MFile>();
static bool changed = false;
static bool state = false;
const string confPath = "all.conf";
static void Main(string[] args)
{
try
{
if (File.Exists(confPath))
{
listFiles = DeserializeFromXml<List<MFile>>(confPath);
}
if (listFiles == null)
{
listFiles = new List<MFile>();
}
if (args != null)
{
foreach (string arg in args)
{
Cycle(arg);
state = true;
ConsoleWriteLine("*************\t" + arg + "\t****************", ConsoleColor.Red);
}
}
Console.ReadLine();
if (changed)
{
SerializeToXml<List<MFile>>(confPath, listFiles);
}
}
catch (Exception ex)
{
ConsoleWriteLine("Main Exception : " + ex.StackTrace, ConsoleColor.Red);
}
}
static bool Cycle(string path)
{
int fileCount = 0;
int folderCount = 0;
try
{
if (path == null || path == "" || Directory.Exists(path))
{
return false;
}
}
catch (Exception ex)
{
ConsoleWriteLine("Directory.Exists(" + path + ") Exception : " + ex.StackTrace, ConsoleColor.Yellow);
}
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch (Exception ex)
{
ConsoleWriteLine("Directory.GetFiles(" + path + ") Exception : " + ex.StackTrace, ConsoleColor.Yellow);
}
if (files != null && (fileCount = files.Length) > 0)
{
foreach (string file in files)
{
try
{
FileInfo fi = new FileInfo(file);
if (state)
{
List<MFile> ls = listFiles.FindAll((f) => { return fi.Name == fi.Name && f.Length == fi.Length; });
if (ls != null && ls.Count > 0)
{
Console.WriteLine("delete file : " + fi.FullName);
fi.Attributes = fi.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden);
fi.Delete();
fileCount--;
}
}
else
{
MFile mf = new MFile
{
Name = fi.Name,
FullName = fi.FullName,
Length = fi.Length
};
listFiles.Add(mf);
changed = true;
}
}
catch (Exception ex)
{
ConsoleWriteLine("FileInfo.Delete(" + file + ") Exception : " + ex.StackTrace, ConsoleColor.Red);
}
}
}
string[] folders = null;
try
{
folders = Directory.GetDirectories(path);
}
catch (Exception ex)
{
ConsoleWriteLine("Directory.GetDirectories(" + path + ") Exception : " + ex.StackTrace, ConsoleColor.Yellow);
}
if (folders != null && (folderCount = folders.Length) > 0)
{
foreach (string folder in folders)
{
if (Cycle(folder))
{
try
{
DirectoryInfo di = new DirectoryInfo(folder);
if (di.GetFiles().Length == 0 && di.GetDirectories().Length == 0)
{
Console.WriteLine("delete " + di.FullName);
di.Attributes = di.Attributes & ~(FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden);
di.Delete();
folderCount--;
}
}
catch (Exception ex)
{
ConsoleWriteLine("DirectoryInfo.Delete(" + path + ") Exception : " + ex.StackTrace, ConsoleColor.Red);
}
}
}
}
return (folderCount <= 0 && fileCount <= 0);
}
static void ConsoleWriteLine(string msg, ConsoleColor cc)
{
var v = Console.ForegroundColor;
Console.ForegroundColor = cc;
Console.WriteLine(msg);
Console.ForegroundColor = v;
}
public static void SerializeToXml<T>(string filePath, T obj)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
xs.Serialize(writer, obj);
}
}
public static T DeserializeFromXml<T>(string filePath)
{
if (!System.IO.File.Exists(filePath))
throw new ArgumentNullException(filePath + " not Exists");
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)xs.Deserialize(reader);
}
}
//static bool isValidFileContent(string filePath1, string filePath2)
//{
// using (HashAlgorithm hash = HashAlgorithm.Create())
// {
// using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open))
// {
// byte[] hashByte1 = hash.ComputeHash(file1);
// byte[] hashByte2 = hash.ComputeHash(file2);
// string str1 = BitConverter.ToString(hashByte1);
// string str2 = BitConverter.ToString(hashByte2);
// return (str1 == str2);
// }
// }
//}
}
}
刪除重復文件的程序