file processing
-
i have an array of strings that represent filenames with the path. i want to find the last directory that is common between all the files. ex: c:\dir1\dir2\abc\abcd\file.ext c:\dir1\dir2\abc\efgh\file.ext c:\dir1\dir2\abc\efgh\123\file.ext the last common directory would be c:\dir1\dir2\abc\ what would be the easiest way to go about this? thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
-
i have an array of strings that represent filenames with the path. i want to find the last directory that is common between all the files. ex: c:\dir1\dir2\abc\abcd\file.ext c:\dir1\dir2\abc\efgh\file.ext c:\dir1\dir2\abc\efgh\123\file.ext the last common directory would be c:\dir1\dir2\abc\ what would be the easiest way to go about this? thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.
The easiest way would be to post your question and have some write the code for you. Here is the code: private string FindSame(params string[] values) { // Default to nothing string sAns = null; // Start out before any valid value int iSame = -1; // Do we have any strings? if (values.Length > 0) { // Setup for looping bool bLoop = true; // Holding area char cChecking = ' '; // Flag bool bCheckingValid; // Loop while (bLoop) { // Move on to next character iSame++; // Reset flag bCheckingValid = false; //Loop thru the strings for (int iValue = values.GetLowerBound(0); iValue <= values.GetUpperBound(0); iValue++) { // Reached the end? if (iValue >= values[iValue].Length) { // No more soup for you bLoop = false; } else { // Do we have something to compare against? if (!bCheckingValid) { // Pick up comparison cChecking = values[iValue][iSame]; // Flag that we got a value bCheckingValid = true; } else { // Compare to string if (cChecking != values[iValue][iSame]) { // Mismatch! bLoop = false; } } } } } // Get the string sAns = values[values.GetLowerBound(0)].Substring(0, iSame); } // Return return sAns; } and here is a sample call: string sSame = this.FindSame( @"c:\dir1\dir2\abc\abcd\file.ext", @"c:\dir1\dir2\abc\efgh\file.ext", @"c:\dir1\dir2\abc\efgh\123\file.ext");
-
The easiest way would be to post your question and have some write the code for you. Here is the code: private string FindSame(params string[] values) { // Default to nothing string sAns = null; // Start out before any valid value int iSame = -1; // Do we have any strings? if (values.Length > 0) { // Setup for looping bool bLoop = true; // Holding area char cChecking = ' '; // Flag bool bCheckingValid; // Loop while (bLoop) { // Move on to next character iSame++; // Reset flag bCheckingValid = false; //Loop thru the strings for (int iValue = values.GetLowerBound(0); iValue <= values.GetUpperBound(0); iValue++) { // Reached the end? if (iValue >= values[iValue].Length) { // No more soup for you bLoop = false; } else { // Do we have something to compare against? if (!bCheckingValid) { // Pick up comparison cChecking = values[iValue][iSame]; // Flag that we got a value bCheckingValid = true; } else { // Compare to string if (cChecking != values[iValue][iSame]) { // Mismatch! bLoop = false; } } } } } // Get the string sAns = values[values.GetLowerBound(0)].Substring(0, iSame); } // Return return sAns; } and here is a sample call: string sSame = this.FindSame( @"c:\dir1\dir2\abc\abcd\file.ext", @"c:\dir1\dir2\abc\efgh\file.ext", @"c:\dir1\dir2\abc\efgh\123\file.ext");
There are a few ways to better the performance, but that's an exercise for the original poster. I just wanted to add that you should either use
Char.ToLower
before comparing, or something similar. Currently, the code won't recognize the following case (and similar cases):@"c:\dir1\file1.ext"
and@"C:\DIR1\FILE2.ext"
(even one difference in case would cause this to fail). Paths in Windows are case-insensitive.Microsoft MVP, Visual C# My Articles
-
There are a few ways to better the performance, but that's an exercise for the original poster. I just wanted to add that you should either use
Char.ToLower
before comparing, or something similar. Currently, the code won't recognize the following case (and similar cases):@"c:\dir1\file1.ext"
and@"C:\DIR1\FILE2.ext"
(even one difference in case would cause this to fail). Paths in Windows are case-insensitive.Microsoft MVP, Visual C# My Articles
Open Reply to your Private Reply: I agree with you that a lot of people that are interested in becoming a programmer (and even many that "are") do noy belong in this field. Like in any field, you need the drive to look for answers yourself and to read the manual (how old is RTFM now?) and to dig in deeper, and too many are plain too lazy to do so. However, I have met many who do have the drive, and the ability is within them, they just lack that insight of thinking outside themselves, which may be (AND IT IS A BIG MAY BE) triggered by seeing a piece of code. I do not know about you, you may have all the answers and need help from no one, but I have gotten great ideas from reading thru some of these boards (the latest example is the work by Tom Clements on menus here at CP, which gave me insight on how to solve a nasty problem I was facing), and provide the little assistance I humbly do to pay back into the system. Once before I answered someone and it seemed to cause some problem with you, and I apologized, even when I felt that I had done nothing wrong. This time you are telling me not to help people out, and to that my answer is to go play with yourself. I do not believe that this board belongs to you, and if it does, publicly tell everyone so and I will happily go someplace else.
-
The easiest way would be to post your question and have some write the code for you. Here is the code: private string FindSame(params string[] values) { // Default to nothing string sAns = null; // Start out before any valid value int iSame = -1; // Do we have any strings? if (values.Length > 0) { // Setup for looping bool bLoop = true; // Holding area char cChecking = ' '; // Flag bool bCheckingValid; // Loop while (bLoop) { // Move on to next character iSame++; // Reset flag bCheckingValid = false; //Loop thru the strings for (int iValue = values.GetLowerBound(0); iValue <= values.GetUpperBound(0); iValue++) { // Reached the end? if (iValue >= values[iValue].Length) { // No more soup for you bLoop = false; } else { // Do we have something to compare against? if (!bCheckingValid) { // Pick up comparison cChecking = values[iValue][iSame]; // Flag that we got a value bCheckingValid = true; } else { // Compare to string if (cChecking != values[iValue][iSame]) { // Mismatch! bLoop = false; } } } } } // Get the string sAns = values[values.GetLowerBound(0)].Substring(0, iSame); } // Return return sAns; } and here is a sample call: string sSame = this.FindSame( @"c:\dir1\dir2\abc\abcd\file.ext", @"c:\dir1\dir2\abc\efgh\file.ext", @"c:\dir1\dir2\abc\efgh\123\file.ext");
Thank you very much. Sorry it took so long to reply but anyway...I took your basic idea and massaged it abit and came up with this:
private int FindCommonDirectory(string[] files) { string path = ""; foreach (string file in files) { string filePath = file.Substring(0, file.LastIndexOf(dirSep) + 1); if ("" == path) path = filePath; else if (filePath == path){} else if (!filePath.StartsWith(path)) { for (int chr = 0; chr <= filePath.Length; chr++) { if (filePath[chr] != path[chr]) { path = filePath.Substring(0, chr); break; } } } } return path.LastIndexOf(dirSep); }
Basically what it does is gets the directory of the first item and then compares it to the rest of the items, char by char. Every time it finds a shorter directory it then uses that to compare with the rest. Thanks again for your help, Rob -- There are 10 kinds of people. Those who understand binary and those who don't. -
Thank you very much. Sorry it took so long to reply but anyway...I took your basic idea and massaged it abit and came up with this:
private int FindCommonDirectory(string[] files) { string path = ""; foreach (string file in files) { string filePath = file.Substring(0, file.LastIndexOf(dirSep) + 1); if ("" == path) path = filePath; else if (filePath == path){} else if (!filePath.StartsWith(path)) { for (int chr = 0; chr <= filePath.Length; chr++) { if (filePath[chr] != path[chr]) { path = filePath.Substring(0, chr); break; } } } } return path.LastIndexOf(dirSep); }
Basically what it does is gets the directory of the first item and then compares it to the rest of the items, char by char. Every time it finds a shorter directory it then uses that to compare with the rest. Thanks again for your help, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.You're welcome. The idea was to give an example, and let you run with it. Next, pass it forward, when you see a question that you have an answer for, give them a hand.