Guest User

Untitled

a guest
Aug 9th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. how to extract common file path from list of file paths in c#
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. List<string> Files = new List<string>()
  11. {
  12. @"c:abcpqrtmpsampleb.txt",
  13. @"c:abcpqrtmpnew2c1.txt",
  14. @"c:abcpqrtmpb2.txt",
  15. @"c:abcpqrtmpb3.txt",
  16. @"c:a.txt"
  17. };
  18.  
  19. var MatchingChars =
  20. from len in Enumerable.Range(0, Files.Min(s => s.Length)).Reverse()
  21. let possibleMatch = Files.First().Substring(0, len)
  22. where Files.All(f => f.StartsWith(possibleMatch))
  23. select possibleMatch;
  24.  
  25. var LongestDir = Path.GetDirectoryName(MatchingChars.First());
  26. }
  27. }
  28.  
  29. Prepare:
  30. Find the shortest string
  31.  
  32. Repeat:
  33. See if all of the other strings contain it
  34. If so, you're done
  35. If not, remove one or more characters
  36.  
  37. string commonPath(string[] paths) {
  38. // this is a Java notation, I hope it's right in C# as well? Let me know!
  39. string[][] tokens = new string[paths.length][];
  40.  
  41. for(int i = 0; i < paths.Length; i++) {
  42. tokens[i] = paths.Split('\');
  43. }
  44.  
  45. string path = "";
  46.  
  47. for(int i = 0; i < tokens[0].Length; i++) {
  48. string current = tokens[0][i];
  49. for(int j = 1; j < tokens.Length; j++) {
  50. if(j >= tokens[i].Length) return path;
  51. if(current != tokens[i][j]) return path;
  52. }
  53. path = path + current + '\';
  54. }
  55. return path; // shouldn't reach here, but possible on corner cases
  56. }
  57.  
  58. List<string> list1 = new List<string>();
  59. list1.Add(@"c:abcpqrtmpsampleb.txt");
  60. list1.Add(@"c:abcpqrtmpnew2c1.txt");
  61. list1.Add(@"c:abcpqrtmpb2.txt");
  62. list1.Add(@"c:abcpqrtmpb3.txt");
  63. list1.Add(@"c:abcpqrtmptmp2b2.txt");
  64.  
  65. string baseDir = "";
  66. foreach (var item in list1)
  67. {
  68. if (baseDir == "")
  69. baseDir = System.IO.Path.GetDirectoryName(item);
  70. else
  71. {
  72. int index = 0;
  73. string nextDir = System.IO.Path.GetDirectoryName(item);
  74. while (index< baseDir.Length && index<nextDir.Length &&
  75. baseDir[index] == nextDir[index])
  76. {
  77. index++;
  78. }
  79. baseDir = baseDir.Substring(0, index);
  80. }
  81. }
  82. MessageBox.Show(baseDir);
  83.  
  84. using System;
  85. using System.Collections.Generic;
  86. using System.IO;
  87.  
  88. namespace stackoverflow1
  89. {
  90. class MainClass
  91. {
  92. public static void Main (string[] args)
  93. {
  94. List<String> paths=new List<String>();
  95. paths.Add(@"c:abcpqrtmpsampleb.txt");
  96. paths.Add(@"c:abcpqrtmpnew2c1.txt");
  97. paths.Add(@"c:abcpqrtmpb2.txt");
  98. paths.Add(@"c:abcpqrtmpb3.txt");
  99. paths.Add(@"c:abcpqrtmptmp2b2.txt");
  100.  
  101. Console.WriteLine("Found: "+ShortestCommonPath(paths));
  102.  
  103. }
  104.  
  105. private static String ShortestCommonPath(IList<String> list)
  106. {
  107. switch (list.Count)
  108. {
  109. case 0: return null;
  110. case 1: return list[0];
  111. default:
  112. String s=list[0];
  113. while (s.Length>0)
  114. {
  115. bool ok=true;
  116. for (int i=1;i<list.Count;i++)
  117. {
  118. if (!list[i].StartsWith(s))
  119. {
  120. ok=false;
  121. int p=s.LastIndexOf(Path.DirectorySeparatorChar);
  122. if (p<0) return "";
  123. s=s.Substring(0,p);
  124. break;
  125. }
  126. }
  127. if (ok) break;
  128. }
  129. return s;
  130. }
  131. }
  132.  
  133. }
  134. }
  135.  
  136. void Main()
  137. {
  138. string[] paths = new[] { @"c:abcpqrtmpsampleb.txt",
  139. @"c:abcpqrtmpnew2c1.txt",
  140. @"c:abcpqrtmpb2.txt",
  141. @"c:abcpqrtmpb3.txt",
  142. @"c:abcpqrtmptmp2b2.txt"};
  143.  
  144. var test = new List<string>();
  145. var common = paths[0].Split('\').TakeWhile ( segment =>
  146. {
  147. test.Add ( segment );
  148. return paths.All ( path => path.StartsWith ( String.Join ("\", test ) + "\") ) ;
  149. } );
  150.  
  151. Console.WriteLine ( String.Join ("\", common ) );
  152. }
Add Comment
Please, Sign In to add comment