Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how to extract common file path from list of file paths in c#
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- List<string> Files = new List<string>()
- {
- @"c:abcpqrtmpsampleb.txt",
- @"c:abcpqrtmpnew2c1.txt",
- @"c:abcpqrtmpb2.txt",
- @"c:abcpqrtmpb3.txt",
- @"c:a.txt"
- };
- var MatchingChars =
- from len in Enumerable.Range(0, Files.Min(s => s.Length)).Reverse()
- let possibleMatch = Files.First().Substring(0, len)
- where Files.All(f => f.StartsWith(possibleMatch))
- select possibleMatch;
- var LongestDir = Path.GetDirectoryName(MatchingChars.First());
- }
- }
- Prepare:
- Find the shortest string
- Repeat:
- See if all of the other strings contain it
- If so, you're done
- If not, remove one or more characters
- string commonPath(string[] paths) {
- // this is a Java notation, I hope it's right in C# as well? Let me know!
- string[][] tokens = new string[paths.length][];
- for(int i = 0; i < paths.Length; i++) {
- tokens[i] = paths.Split('\');
- }
- string path = "";
- for(int i = 0; i < tokens[0].Length; i++) {
- string current = tokens[0][i];
- for(int j = 1; j < tokens.Length; j++) {
- if(j >= tokens[i].Length) return path;
- if(current != tokens[i][j]) return path;
- }
- path = path + current + '\';
- }
- return path; // shouldn't reach here, but possible on corner cases
- }
- List<string> list1 = new List<string>();
- list1.Add(@"c:abcpqrtmpsampleb.txt");
- list1.Add(@"c:abcpqrtmpnew2c1.txt");
- list1.Add(@"c:abcpqrtmpb2.txt");
- list1.Add(@"c:abcpqrtmpb3.txt");
- list1.Add(@"c:abcpqrtmptmp2b2.txt");
- string baseDir = "";
- foreach (var item in list1)
- {
- if (baseDir == "")
- baseDir = System.IO.Path.GetDirectoryName(item);
- else
- {
- int index = 0;
- string nextDir = System.IO.Path.GetDirectoryName(item);
- while (index< baseDir.Length && index<nextDir.Length &&
- baseDir[index] == nextDir[index])
- {
- index++;
- }
- baseDir = baseDir.Substring(0, index);
- }
- }
- MessageBox.Show(baseDir);
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace stackoverflow1
- {
- class MainClass
- {
- public static void Main (string[] args)
- {
- List<String> paths=new List<String>();
- paths.Add(@"c:abcpqrtmpsampleb.txt");
- paths.Add(@"c:abcpqrtmpnew2c1.txt");
- paths.Add(@"c:abcpqrtmpb2.txt");
- paths.Add(@"c:abcpqrtmpb3.txt");
- paths.Add(@"c:abcpqrtmptmp2b2.txt");
- Console.WriteLine("Found: "+ShortestCommonPath(paths));
- }
- private static String ShortestCommonPath(IList<String> list)
- {
- switch (list.Count)
- {
- case 0: return null;
- case 1: return list[0];
- default:
- String s=list[0];
- while (s.Length>0)
- {
- bool ok=true;
- for (int i=1;i<list.Count;i++)
- {
- if (!list[i].StartsWith(s))
- {
- ok=false;
- int p=s.LastIndexOf(Path.DirectorySeparatorChar);
- if (p<0) return "";
- s=s.Substring(0,p);
- break;
- }
- }
- if (ok) break;
- }
- return s;
- }
- }
- }
- }
- void Main()
- {
- string[] paths = new[] { @"c:abcpqrtmpsampleb.txt",
- @"c:abcpqrtmpnew2c1.txt",
- @"c:abcpqrtmpb2.txt",
- @"c:abcpqrtmpb3.txt",
- @"c:abcpqrtmptmp2b2.txt"};
- var test = new List<string>();
- var common = paths[0].Split('\').TakeWhile ( segment =>
- {
- test.Add ( segment );
- return paths.All ( path => path.StartsWith ( String.Join ("\", test ) + "\") ) ;
- } );
- Console.WriteLine ( String.Join ("\", common ) );
- }
Add Comment
Please, Sign In to add comment