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);
                });
            }           
        }

 

你可能感兴趣的:(C# copy files from source directory to destination file and rename repeated files and does not override)