fork(3) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.IO;
  5.  
  6. public class Test
  7. {
  8. public static string AddIncrementFileNameSuffix(string path, IList<string> paths, string separator)
  9. {
  10. int num = 0;
  11. string[] tokens = path.Split(new[] { separator }, StringSplitOptions.None);
  12. string strNum = tokens.Last();
  13. int.TryParse(strNum, out num);
  14. string fileName = path;
  15. string dir = Path.GetDirectoryName(path);
  16. string ext = Path.GetExtension(path);
  17.  
  18. var dups = paths.Where(n => n.Equals(fileName, StringComparison.OrdinalIgnoreCase));
  19. while (dups.Any())
  20. {
  21. fileName = Path.Combine(dir, tokens.First() + separator + ++num + ext);
  22. }
  23.  
  24. paths.Add(fileName);
  25. return fileName;
  26. }
  27.  
  28. public static void Main()
  29. {
  30. var SourceFiles = new List<String>() { @"C:\Temp\test_1", @"C:\Temp\test_2" };
  31. var lsNameList = new List<String>() { @"C:\Temp\test_1", @"C:\Temp\test_2", @"C:\Temp\test_3" };
  32.  
  33.  
  34. IEnumerable<String> newPaths = SourceFiles
  35. .Select(fn => AddIncrementFileNameSuffix(fn, lsNameList,"_"));
  36.  
  37. Console.Write(string.Join(Environment.NewLine, newPaths.ToArray()));
  38. }
  39. }
Success #stdin #stdout 0.07s 34840KB
stdin
Standard input is empty
stdout
C:\Temp\test_4
C:\Temp\test_5