星空网 > 软件开发 > ASP.net

删除重复文件的程序

删除重复的文件功能

使用方法:

建一个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 = DeserializeFrom(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)        {          SerializeTo<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 SerializeTostring filePath, T obj)    {      using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))      {        System.= new System.typeof(T));        xs.Serialize(writer, obj);      }    }    public static T DeserializeFromstring filePath)    {      if (!System.IO.File.Exists(filePath))        throw new ArgumentNullException(filePath + " not Exists");      using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))      {        System.= new System.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);    //    }    //  }    //}   }}

删除重复文件的程序

 




原标题:删除重复文件的程序

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

90后国内电商老手转型跨境,6个月狂销500万美金!:https://www.ikjzd.com/articles/1679078243920715777
做慈善?Prime Day上半场销售额70万美金:https://www.ikjzd.com/articles/1679082749546930177
全新!Keith律师再代理版权画, Howard Raymond Green, Jr版权画首次维权!:https://www.ikjzd.com/articles/1679099916669624322
为什么现在是做跨境电商的最佳时期?:https://www.ikjzd.com/articles/16791
香港公司税务申报需要关注的几个事项:https://www.ikjzd.com/articles/1679296464418447362
不刷单不刷评,偷师亚马逊大卖们的亚马逊运营策略!:https://www.ikjzd.com/articles/16793
3月独立站新品观察:比基尼、连衣裙、凉鞋、止汗霜等夏季新品热推! :https://www.kjdsnews.com/a/1836553.html
3月独立站新品观察:比基尼、连衣裙、凉鞋、止汗霜等夏季新品热推! :https://www.goluckyvip.com/news/188216.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流