I've SQLite3 database table where I store a list of paths like this:
/mnt/sdcard/folder1/a/b/file1
/mnt/sdcard/folder1/a/b/file2
/mnt/sdcard/folder1/a/b/file3
/mnt/sdcard/folder1/a/b/file4
/mnt/sdcard/folder1/a/b/file5
/mnt/sdcard/folder1/e/c/file6
/mnt/sdcard/folder2/d/file7
/mnt/sdcard/folder2/d/file8
/mnt/sdcard/file9
What I want to do is to find the common root of these paths and to get the list of first level folder (unique) of that common root.
For example
First run: parent root = null (it's the first run) common root -> /mnt/sdcard/ List of folders - folder1 - folder2
Second run (now parent root will be /mnt/sdcard/folder1/) common root -> /mnt/sdcard/folder1/ (same as parent root) List of folders - a - e
Second run (now parent root will be /mnt/sdcard/folder1/a/) common root -> /mnt/sdcard/folder1/a/b (same as parent root) List of folders -> empty (I'll get files)
Is there a way to do those filters by db or I have I to do that by code?
This question is made because I need to provide a Folder View of Android Music Library that store paths in the song db record.
Take a look at http://rosettacode.org/wiki/Find_common_directory_path
It's implemented in some programming languages.
This is the Java example I've tested and used for my purposes.
public class CommonPath {
public static String commonPath(String... paths){
String commonPath = "";
String[][] folders = new String[paths.length][];
for(int i = 0; i < paths.length; i++){
folders[i] = paths[i].split("/"); //split on file separator
}
for(int j = 0; j < folders[0].length; j++){
String thisFolder = folders[0][j]; //grab the next folder name in the first path
boolean allMatched = true; //assume all have matched in case there are no more paths
for(int i = 1; i < folders.length && allMatched; i++){ //look at the other paths
if(folders[i].length < j){ //if there is no folder here
allMatched = false; //no match
break; //stop looking because we've gone as far as we can
}
//otherwise
allMatched &= folders[i][j].equals(thisFolder); //check if it matched
}
if(allMatched){ //if they all matched this folder name
commonPath += thisFolder + "/"; //add it to the answer
}else{//otherwise
break;//stop looking
}
}
return commonPath;
}
public static void main(String[] args){
String[] paths = { "/home/user1/tmp/coverage/test",
"/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members"};
System.out.println(commonPath(paths));
String[] paths2 = { "/hame/user1/tmp/coverage/test",
"/home/user1/tmp/covert/operator",
"/home/user1/tmp/coven/members"};
System.out.println(commonPath(paths2));
}
}
I just had to do the same thing, but in C#. Here's my solution to finding a common root directory. You should be able to adapt it.
public static string GetCommonRoot( params string[] paths )
{
int minDepth = 999;
List<string[]> parsedPaths = new List<string[]>();
foreach( string p in paths )
{
string[] parts = p.Split( '\\' );
parsedPaths.Add( parts );
minDepth = parts.Length < minDepth ? parts.Length : minDepth;
}
StringBuilder sb = new StringBuilder();
for( int i = 0; i < minDepth; i++ )
{
List<string> tmp = new List<string>();
foreach( string[] dir in parsedPaths )
{
tmp.Add( dir[ i ].ToLower() );
}
if( !AllEqual( tmp.ToArray() ) )
break;
sb.AppendFormat( "{0}\\", tmp[ 0 ] );
}
return sb.ToString();
}
private static bool AllEqual( params string[] strings )
{
bool rv = true;
for( int i = 1; i < strings.Length; i++ )
rv &= strings[ 0 ] == strings[ i ];
return rv;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With