Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-17
5
Medium Priority
?
199 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

609 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