Solved

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

Posted on 2004-09-17
5
191 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

7 Experts available now in Live!

Get 1:1 Help Now