G'day all,

I'm currently a little stuck with my application. The strings i'm working with are a filepath and then a filename, what I want is to compare the filename with the filepath and then output the directory that most matches with the filename. My current code below works like the following:

Filepath: C:\Program Files\ABCDE Folder\system
Filename: ABCDE.exe
Return: ABCDE Folder

However it wouldn't work if the first few characters don't match, for example:

Filepath: C:\Program Files\ABCDE Folder\system
Filename: ACDE.exe or AAABBBCCDDEE.exe
Return: null

What i'm aiming for is to find out a way to get some sort of percentage of "accuracy" of the filename to any given directory within the filepath. Thanks again for any input. :D

public string FindProgramName(string FullPath)
        {
            string filename;
            string PathuntilFile;

            filename = Path.GetFileNameWithoutExtension(FullPath);
            PathuntilFile = Path.GetDirectoryName(FullPath);

            filename = filename.ToLower();
			string[] pathComponents = PathuntilFile.ToLower().Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string comp in pathComponents)
			{
				int matches = 0;
				for (int i = 0;  i < comp.Length && i < filename.Length; i++)
				{
					if (comp[i] == filename[i])
					{
						if (++matches == 3)
							return comp;
					}
					else
					{
						break;
					}
				}
			}
			return "";
		}

Sounds like you need regular expressions.
Here's a succinct tutorial I came across. They are toting some product or something but it has a few good examples of what you might need towards the bottom of the page.
http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx

Here's the MSDN page that gives a couple of oversimplified examples but that should give you some of the syntax.
http://msdn.microsoft.com/en-us/library/ms228595(VS.80).aspx

I would imagine there's some linguistics material out there that deals with the kind of statistical processing of which you speak but it might be overkill for this.

Also, if your going to be trying out regex, i found this online tool to be quite handy. Alternatively, you can download a free version of Expresso...great tool for putting together complex regular expressions, you can break it down into sections etc. Helped me get a handle on them :)

Thank guys for all your replies. I'll check it out and see if I can get it to work. I'll keep you updated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.