|
-
Dec 31st, 2005, 12:57 AM
#1
Thread Starter
PowerPoster
efficiency
Hi
I am trying to look for a more effecient, better way of doing this.
Let's say we have a directory, in this directory there are various directories.
Now, most of the directories will be a numeric/integer named directory ie:
1
2
3
65
etc...
however this directory may also contain non integer/numeric names (such as "test" or "h3ll0" etc...)
What I want to do is to obtain a list of ONLY the integer directory name.
Of course, I could use a for each on each directory name and check to see if the current Char in the current directory name is a digit/numeric or not but that i do not think is the most efficient way as there maybe several hundred directories therefore it will have some performance degration if using a Char.IsDigit/IsNumeric method.
What is the best way?
-
Dec 31st, 2005, 07:00 AM
#2
Re: efficiency
I would use this code:
VB Code:
System.IO.Directory.GetDirectories("C:\MyPath\")
To get all the directories and throw them into an array (You can use the AddRange() method of an ArrayList).
Then run the IsNumeric() function on each string
VB Code:
public bool IsNumeric(string ToBeChecked)
{
double dbl = 0;
return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
}
I think that's the best way to do it. You could try to check if they're numeric or not as you put them into an array or whatever you want.
-
Dec 31st, 2005, 07:01 AM
#3
Re: efficiency
I used the following code to read in 704 directories and compare all of them:
VB Code:
Tester.frmTest.HiPerfTimer pt = new Tester.frmTest.HiPerfTimer();
pt.Start();
int numberOfDirs = 0;
string dirBase = "K:\\Test\\";
System.Collections.ArrayList dirs = new ArrayList(System.IO.Directory.GetDirectories(dirBase));
foreach (string s in dirs)
{
if (IsNumeric(s.Replace(dirBase, "")))
{
++numberOfDirs;
}
}
pt.Stop();
MessageBox.Show(pt.Duration.ToString());
It took .012 seconds
-
Dec 31st, 2005, 12:42 PM
#4
Thread Starter
PowerPoster
Re: efficiency
Thanks for that guys
the thing is - the method.TryParse() - if it cannot parse it, it would through an exception rather than a bool...
-
Dec 31st, 2005, 04:43 PM
#5
Fanatic Member
Re: efficiency
 Originally Posted by Techno
Thanks for that guys
the thing is - the method.TryParse() - if it cannot parse it, it would through an exception rather than a bool...
You should be to handle that with a TRY...CATCH construct. Method documentation can give you details on the exceptions that are likely to be thrown too. If you look that up you can catch the exact exception rather than just a general exception as well.
-
Jan 1st, 2006, 12:53 PM
#6
Re: efficiency
 Originally Posted by Techno
Thanks for that guys
the thing is - the method.TryParse() - if it cannot parse it, it would through an exception rather than a bool...
The whole idea of the TryParse method(s) is that it doesn't throw an exception if the operation fails. If the method returns false then the string could not be parsed. If the method returns True then the parsed value is stored in the fourth argument, which is output only. I'd also be inclined to use NumberStyles.Integer for your purposes. Note that all primitive types have a TryParse method in .NET 2.0.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|