-
Oct 13th, 2009, 04:36 PM
#1
Thread Starter
Lively Member
[RESOLVED] Quicker way of doing this ...?
Hi Guys,
Is there a quicker way to check directories?
For example I want to check if the folder "A Folders" contains folders from 000A all the way to 999A then lists the folder that are missing?
Do you think this is possible?
I could do the following:
If Directory.Exists("z:\A Folders\000A") Then
'do nothing
Else
'list this folder in a list box
End If
But I'd have to do this for every folder and wondered if there was a quicker way.
Any help appreciated!
Br00nage
-
Oct 13th, 2009, 04:57 PM
#2
Re: Quicker way of doing this ...?
It's going to require testing no matter what you do. I assume that you are asking the question because the performance is too slow. My first thought would be to get all the directory names as strings, strip off the first three characters, convert to integer, add to a list of Integer, sort the list, then go through it looking for omissions. Would that be faster? It sounds kind of long when written out, but the whole "strip off the first three characters and convert to integer" thing is a single statement that can be put into the List.Add. Therefore, I would say that the whole thing comes down to whether or not you can get the names of all the objects in A Folders more quickly than you can do the Exists. As to that, I have no idea.
My usual boring signature: Nothing
-
Oct 13th, 2009, 05:10 PM
#3
Addicted Member
Re: Quicker way of doing this ...?
One think I would try is to store a list of directories in "z:\A Folders\" in a array. Then you could do a for-each loop with x as a counter going from 0 to 999.
For each iteration, you would make a string using a select case-statement. If x is less than 10, the string would be like this:
Code:
mystring = "00" & x & "A"
Then you could just check if your array contains that string. That code runs in no-time on my computer
EDIT: I measured it right now, and it takes around 0.002 seconds to check C:\Windows
-
Oct 13th, 2009, 05:22 PM
#4
Thread Starter
Lively Member
Re: Quicker way of doing this ...?
Thanks for your replies, Erik could you expand a bit please, could you possibly show me some code for the array etc ...?
Thanks,
Br00nage
-
Oct 13th, 2009, 05:34 PM
#5
Re: Quicker way of doing this ...?
Shaggy, I'm not sure but I think he meant is there a faster than than copying and pasting that code 999 times to check for each one. In other words he doesn't know about arrays and looping.
-
Oct 13th, 2009, 05:37 PM
#6
Re: Quicker way of doing this ...?
Checking for 999 folders will not take long at all. Checking for 9999 folders probably wouldn't take that long either. Just use a normal For loop.
-
Oct 13th, 2009, 05:40 PM
#7
Re: Quicker way of doing this ...?
Sorry, I didn't see your post, I was posting. If you didn't know about looping or anything, this is just in case.
vb Code:
Dim i As Integer Dim l As New List(Of String) For i = 0 To 999 If Not IO.Directory.Exists(i.ToString() & "A") Then l.Add(i.ToString() & "A") Next
If l.Count is 0, all the directories exist. Otherwise, the list contains the missing folders.
-
Oct 13th, 2009, 05:44 PM
#8
Thread Starter
Lively Member
Re: Quicker way of doing this ...?
Thank you Minitech, I'll play with this code.
Kind regards,
Br00nage
-
Oct 13th, 2009, 06:00 PM
#9
Thread Starter
Lively Member
Re: Quicker way of doing this ...?
Sorry to bother you Minitech, but how would I use the code you provided:
Code:
Dim i As Integer
Dim l As New List(Of String)
For i = 0 To 999
If Not IO.Directory.Exists(i.ToString() & "A") Then l.Add(i.ToString() & "A")
Next
on a certain directory and how do I list the missing directories on the form?
Thanks for your help,
Br00nage
-
Oct 13th, 2009, 06:02 PM
#10
Re: Quicker way of doing this ...?
Looks like Shaggy was right:
vb.net Code:
Public Sub Test() Dim sw As New Stopwatch() '============ 'First method: '============ sw.Start() Dim directories() As Integer = IO.Directory.GetDirectories("C:\test\Folders") _ .Select(Function(directory) _ CInt(IO.Path.GetFileName(directory). _ TrimEnd("A"c))).ToArray() Dim missing As New List(Of String)() For counter As Integer = 0 To 999 If Not directories.Contains(counter) Then missing.Add(counter.ToString("000A")) End If Next sw.Stop() 'Result: ~6351530 on dual core Console.WriteLine(sw.ElapsedTicks) sw.Reset() '============= 'Second method: '============= sw.Start() Dim path As String = "C:\test\Folders\" missing = New List(Of String)() For counter As Integer = 0 To 999 If Not IO.Directory.Exists(String.Format(path & "{0}", _ counter.ToString("000A"))) Then missing.Add(counter.ToString("000A")) End If Next sw.Stop() 'Result: ~45021390 on dual core Console.WriteLine(sw.ElapsedTicks) End Sub
Can't think of a more efficient way for either.
-
Oct 13th, 2009, 06:06 PM
#11
Re: Quicker way of doing this ...?
vb.net Code:
Dim i As Integer Dim l As New List(Of String) For i = 0 To 999 If Not IO.Directory.Exists("C:\MyDir\" & i.ToString() & "A") Then l.Add(i.ToString() & "A") Next
l.ToArray() returns an array of strings. Add this to your ListBox. (ListBox1.AddRange(l.ToArray()))
-
Oct 13th, 2009, 06:31 PM
#12
Thread Starter
Lively Member
Re: Quicker way of doing this ...?
Sorry guys, I'm having some trouble with this:
Code:
Dim i As Integer
Dim l As New List(Of String)
For i = 0 To 999
If Not IO.Directory.Exists("C:\test\Folders\" & i.ToString() & "A") Then l.Add(i.ToString() & "A")
ListBox1.Items.AddRange(l.ToArray())
Next
This seems to be working although, its listing number like 1A, 0A, etc when I only want from 000A to 999A.
Any thoughts?
Br00nage
-
Oct 13th, 2009, 09:11 PM
#13
Addicted Member
Re: Quicker way of doing this ...?
ListBox1.Items.AddRange(l.ToArray()) should come after the Next
Code:
Dim i As Integer
Dim l As New List(Of String)
For i = 0 To 999
If Not IO.Directory.Exists("C:\test\Folders\" & i.ToString() & "A") Then l.Add(i.ToString() & "A")
Next
ListBox1.Items.AddRange(l.ToArray())
-
Oct 14th, 2009, 01:20 AM
#14
Thread Starter
Lively Member
Re: Quicker way of doing this ...?
I have the following code :
Code:
Dim i As Integer
Dim l As New List(Of String)
i = Text.Length = 3
For i = 1 To 999
If Not IO.Directory.Exists("C:\test\Folders\" & i.ToString() & "A") Then l.Add(i.ToString() & "A")
Next
ListBox1.Items.AddRange(l.ToArray())
but the ListBox1 list folder from 0A up to 100A when I really need to list only folder from 000A to 999A and report back with the missing folders if they do not exist.
I've tried adding a Text.Length but this doesn't seem to be working.
-
Oct 14th, 2009, 02:10 AM
#15
Hyperactive Member
Re: Quicker way of doing this ...?
All you need to do is to take i, convert it to a string, then padLeft with "0" for a total length of 3. In the example below I use a temporary variable TempStr for clarity's sake, but you can even omit it and go straight with the expression i.toString().PadLeft(3,"0"c) replacing reference to TempStr in the If statement.
Code:
Dim i As Integer
Dim l As New List(Of String)
Dim TempStr as string = ""
'i = Text.Length = 3 'what on earth is this supposed to do???
For i = 1 To 999
TempStr = i.toString().PadLeft(3,"0"c)
If Not IO.Directory.Exists("C:\test\Folders\" & TempStr & "A") Then l.Add(i.ToString() & "A")
Next
ListBox1.Items.AddRange(l.ToArray())
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
-
Oct 14th, 2009, 03:47 AM
#16
Addicted Member
Re: Quicker way of doing this ...?
The way I was talking about is this:
vb.net Code:
Dim missing As New List(Of String)
Dim ary() As String = System.IO.Directory.GetDirectories("C:\Windows\")
Dim str As String = ""
For x As Integer = 0 To 999
Select Case x
Case Is < 100
str = "0" & x.ToString & "A"
Case Is < 10
str = "00" & x.ToString & "A"
Case Else
str = x.ToString & "A"
End Select
If Not ary.Contains(str) Then missing.Add(str)
Next
If it's about pure speed, this code is by far the fastest (around ten times faster than anyone here). But would you notice this as a user? I don't think so... Whatever code you would use, it would be so fast that you wouldn't even notice. I think it all comes down to a matter of taste, and some of the codes here are a bit neater, in my opinion
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
|