刪除重複文件的程序

刪除重複的文件功能網絡

使用方法:ide

建一個BAT文件,如1.bat,裏面寫入:RemoveDuplicate.exe    path1     path2   (或者在命令行下輸入以上內容)spa

其中path1表示原文件夾,path2表示要檢測和刪除的文件夾命令行

例如文件夾path1中有:1.txt、2.txt、3.txt、4.txt、5.txt設計

例如文件夾path2中有:4.txt、5.txt、6.txt、7.txt、8.txtcode

(path1和path2中4.txt、5.txt是同名同大小的文件)blog

 

執行  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);
        //        }
        //    }
        //} 
    }
}
刪除重複文件的程序
相關文章
相關標籤/搜索