Solved

Determine if a text of a filename means it is an older file

Posted on 2004-09-17
5
193 Views
Last Modified: 2010-04-15
if you have a string "blah_1-7-9-3.zip"  how can I compare it to "blah_1-7-10-1.zip" and "blah_2-1-0-1.zip" to determine that 2-1-0-1 is the highest build number.

I know i should get just the numbers and compare them 1 at a time to see which is highest, but I cant figure out how to do it.
0
Comment
Question by:Rowdyone52
5 Comments
 

Author Comment

by:Rowdyone52
ID: 12088397
I just decided to add them into a ArrayList and then sort them, anyone see a problem in doing this?
0
 
LVL 5

Accepted Solution

by:
tomasX2 earned 500 total points
ID: 12088926
YES
                  System.Collections.ArrayList list =new System.Collections.ArrayList();
                  list.Add("a_1_0_1");
                  list.Add("a_1_0_11");
                  list.Add("a_1_0_2");
                  list.Add("a_1_0_3");
                  list.Add("a_1_0_17");

                  list.Sort();

                  foreach( object a in list )
                  {
                        System.Diagnostics.Debug.WriteLine(a);
                  }


OUTPUT
a_1_0_1
a_1_0_11
a_1_0_17
a_1_0_2
a_1_0_3


You could probably process with a regular expression or something like....

            private void DoCompare()
            {
                  string s1 = "blah_1-7-9-3.zip";
                  string s2 = "blah_1-7-10-1.zip";
                  System.Version v = GetVersion(s1,"blah_","-");
                  System.Version v1 = GetVersion(s2,"blah_","-");
                  int c = v.CompareTo(v1);
                  int d = v1.CompareTo(v);
                  int e = v.CompareTo(v);

                  System.Diagnostics.Debug.WriteLine(c);
                  System.Diagnostics.Debug.WriteLine(d);
                  System.Diagnostics.Debug.WriteLine(e);
            }

            private System.Version GetVersion(string version, string prefix, string delimiter)
            {                  
                  version = version.Replace(prefix,"");
                  int indexOfPeriod = version.IndexOf('.');
                  version = version.Remove(indexOfPeriod,version.Length - indexOfPeriod);
                  version = version.Trim().Replace(delimiter,".");
                  return new System.Version(version);
            }

OUTPUT

-1
1
0

0
 
LVL 18

Expert Comment

by:armoghan
ID: 12091096
I think there may be some problems while doing the String comparison as the date can be in differnt formats
secondly
a date in year 1900 may create problem

So I would suggest that first create the data object use DateTime.Compare

http://www.superdotnet.com/Article.aspx?ArticleID=79
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12091662
how about

int VersionCompare(string src,string des)
{
  int  i;
  src=src.Substring(src.IndexOf('_')+1,src.IndexOf('.')-src.IndexOf('_')-1);
  des=des.Substring(des.IndexOf('_')+1,des.IndexOf('.')-des.IndexOf('_')-1);

  string srcversion[]=src.Split(new Char[]{'-'});
  string desversion[]=src.Split(new Char[]{'-'});
 
  i=0;
  while((i<srcversion.Count)&&(i<desversion.Count))
  {
     if(int.Parse(srcversion[i])!=int.Parse(desversion[i])) break;
     i++;
  }
  if((i<srcversion.Count)||(i<desversion.Count))
      throw new Exception("the version string is crashed!")//not the same version form
  if(i>=srcversion.Count) return 0;//same version
  if(int.Parse(srcversion[i])>int.Parse(desversion[i])) return -1;//src is small
  return 1;
}
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 12101028
Simple solution is to write a function which will give you the build number as a comparable number.
if you know for sure the format of the build number string.
ie., "blah_1-7-10-1.zip" should give you 01071001 and "blah_2-1-0-1.zip"  will give 02010001

private integer GetBuildNumber( string sBuildstring)
{
string delimStr = "_";
char [] delimiter = delimStr.ToCharArray();
string[] sArray, sArray2, sArray3;
sArray = sBuildString.Split(delimiter);
string sTemp = sArray[1];
delimStr = ".";
delimiter = delimStr.ToCharArray();
sArray2 = sTemp.Split(delimiter);
delimStr = "-";
delimiter = delimStr.ToCharArray();
sArray3 = sArray2[0]..Split(delimiter);

return (int.Parse(sArray3[0])*1000000) + (int.Parse(sArray3[1])*10000) + (int.Parse(sArray3[2])*100) +(int.Parse(sArray3[3])*1)

}
I have not compiled this.. but this is the logic..
Cheers...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question