Solved

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

Posted on 2004-09-17
5
192 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String manipulation 15 64
Allow space in this pattern 2 46
Runtime Error 2 27
About delegates in c sharp 3 23
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now