Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. file processing

file processing

Scheduled Pinned Locked Moved C#
data-structuresquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rob Tomson
    wrote on last edited by
    #1

    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.

    J 1 Reply Last reply
    0
    • R Rob Tomson

      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.

      J Offline
      J Offline
      je_gonzalez
      wrote on last edited by
      #2

      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");

      H R 2 Replies Last reply
      0
      • J je_gonzalez

        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");

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • H Heath Stewart

          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

          J Offline
          J Offline
          je_gonzalez
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • J je_gonzalez

            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");

            R Offline
            R Offline
            Rob Tomson
            wrote on last edited by
            #5

            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.

            J 1 Reply Last reply
            0
            • R Rob Tomson

              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.

              J Offline
              J Offline
              je_gonzalez
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups