Programs that delete duplicate files and programs that delete duplicate files

Source: Internet
Author: User

Programs that delete duplicate files and programs that delete duplicate files

Delete duplicate files

Usage:

Create a BAT file, such as 1.bat, and write: RemoveDuplicate.exe path1 path2 (or enter the above content under the command line)

Where path1 indicates the original folder and path2 indicates the folder to be checked and deleted

For example, the folder path1 contains: 1.txt00002.txt00003.txt00004.txt00005.txt

For example, the folder path2 contains 4.txt00005.txt00006.txt00007.txt00008.txt.

(Path1and path24.txt00005.txt are files of the same name and size)

 

Run RemoveDuplicate.exe path1 path2.

After:

The folder path1 contains: 1.txt00002.txt00003.txt00004.txt00005.txt

The folder path2 contains: 6.txt00007.txt00008.txt

4.txt00005.txt in the folder path2will be deleted.

 

Purpose of writing this method:

I have two development machines and one home machine. at ordinary times, many source code and design files are transferred from each machine and copied many copies. Recently, I found that the capacity of one of the development machines is full, I want to repeat the source code and design documents of the two development machines and the home machine (the two development machines have the monitoring service similar to the network worm, monitors and downloads A lot of resources on the network. Only one of the resources is retained, such as development machine A, development machine B, and home machine C. The "development machine A" is used as the basis, delete repeated source code and various documents on "Developer B" and "home machine C.

-----------------------------------------------------------

You can add this program to "developer A" and execute RemoveDuplicate.exe pathA on the console. In this example, pathA indicates the basic path (the source code and various documents are used as references ), an all. conf file, which records the information (name, path, size) of all files under the "developer A" pathA path );

For example, put removeduplicate.exe under the drive D of "developer ".

On the console, enter the command cd \ d: \ to switch to drive d.

In the console, enter RemoveDuplicate.exe d: \ or RemoveDuplicate.exe "d :\"

An all. conf file is generated under drive D.

 

-------------------------------------------------------------

Then, removeduplicate.exe and all. put the conf file in "Developer B" and execute RemoveDuplicate.exe "an exists directory" pathB on the console. "an exists directory" indicates a file path that does not exist, you can directly write "" (empty strings do not omit the quotation marks), or write a non-existent path such as aaaaaaaaa. pathB indicates the folder path that "Developer B" needs to be checked and deleted;

For example, put removeduplicate.exe under the drive D of "Developer B ".

On the console, enter the command cd \ d: \ to switch to drive d.

In the console, enter RemoveDuplicate.exe "" d: \ e: \ or RemoveDuplicate.exe "d: \" "e :\"

The files in the d: \ and e: \ paths of "development machine B" and all. conf will be deleted.

On the console, enter RemoveDuplicate.exe d: \ to include all files under drive d of "Developer B" in all. conf.

Then, enter RemoveDuplicate.exe e: \ on the console to include all files under the e drive letter of "Developer B" in all. conf.

 

-------------------------------------------------------------

Then, put the RemoveDuplicate.exe and all. conf files in the "home machine C", and execute RemoveDuplicate.exe "an exists directory" pathC in the console (the rest are the same as above );

For example, put removeduplicate.exe under the drive D of "household machine C ".

On the console, enter the command cd \ d: \ to switch to drive d.

In the console, enter RemoveDuplicate.exe "" e: \ or RemoveDuplicate.exe "" e :\"

The files in the e: \ path of the "home machine C" and all. conf will be deleted.

 

 

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);        //        }        //    }        //}     }}
Programs that delete duplicate files

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.