?
Solved

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

Posted on 2004-09-17
5
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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