C# copy files from source directory to destination file and rename repeated files and does not override

复制代码
  static void CopyFiles()
        {
            string sourceDir = @"D:\C\ll";
            string destDir = @"D:\LL";
            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);
            }

           string[] mp3Files= Directory.GetFiles(sourceDir, "*.mp3", SearchOption.AllDirectories);
           if(mp3Files!=null && mp3Files.Any())
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                List<string> repeatedList = new List<string>();
                foreach (string mp3 in mp3Files)
                {                  
                    string mp3FileName = Path.GetFileName(mp3);
                    string newMp3FileName = Path.GetFileName(mp3);
                    if (dic.ContainsKey(mp3FileName))
                    {                         
                        string guid = Guid.NewGuid().ToString().Substring(0, 6);
                        newMp3FileName = Path.GetFileNameWithoutExtension(mp3FileName) + guid + ".mp3";
                        dic.Add(newMp3FileName, newMp3FileName);
                        string repeatedMsg = $"mp3:{mp3},newMp3FileName:{newMp3FileName}";
                        repeatedList.Add(repeatedMsg);
                    }
                    else
                    {
                        dic.Add(mp3FileName, mp3FileName);
                    }

                    string newMp3FullName = Path.Combine(destDir, newMp3FileName);
                    File.Copy(mp3, newMp3FullName, false);
                    Console.WriteLine($"oldmp3:{mp3},newMp3FullName:{newMp3FullName}");
                }
                dic = null;

                Console.WriteLine($"\n\n There are {repeatedList.Count} Repeated Msg:");
                repeatedList.ForEach(x =>
                {
                    Console.WriteLine(x);
                });
            }           
        }
复制代码

 

posted @   FredGrit  阅读(291)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示