PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Quicker way of doing this ...?-VBForums
Results 1 to 16 of 16

Thread: [RESOLVED] Quicker way of doing this ...?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    73

    Resolved [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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,918

    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

  3. #3
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    73

    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

  5. #5
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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.

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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:
    1. Dim i As Integer
    2. Dim l As New List(Of String)
    3. For i = 0 To 999
    4.       If Not IO.Directory.Exists(i.ToString() & "A") Then l.Add(i.ToString() & "A")
    5. Next

    If l.Count is 0, all the directories exist. Otherwise, the list contains the missing folders.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    73

    Re: Quicker way of doing this ...?

    Thank you Minitech, I'll play with this code.

    Kind regards,
    Br00nage

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    73

    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

  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Quicker way of doing this ...?

    Looks like Shaggy was right:

    vb.net Code:
    1. Public Sub Test()
    2.  
    3.     Dim sw As New Stopwatch()
    4.     '============
    5.     'First method:
    6.     '============
    7.     sw.Start()
    8.     Dim directories() As Integer = IO.Directory.GetDirectories("C:\test\Folders") _
    9.                                          .Select(Function(directory) _
    10.                                                     CInt(IO.Path.GetFileName(directory). _
    11.                                                             TrimEnd("A"c))).ToArray()
    12.     Dim missing As New List(Of String)()
    13.     For counter As Integer = 0 To 999
    14.         If Not directories.Contains(counter) Then
    15.             missing.Add(counter.ToString("000A"))
    16.         End If
    17.     Next
    18.     sw.Stop()
    19.     'Result: ~6351530 on dual core
    20.     Console.WriteLine(sw.ElapsedTicks)
    21.  
    22.     sw.Reset()
    23.  
    24.     '=============
    25.     'Second method:
    26.     '=============
    27.     sw.Start()
    28.     Dim path As String = "C:\test\Folders\"
    29.     missing = New List(Of String)()
    30.     For counter As Integer = 0 To 999
    31.         If Not IO.Directory.Exists(String.Format(path & "{0}", _
    32.                                                  counter.ToString("000A"))) Then
    33.             missing.Add(counter.ToString("000A"))
    34.         End If
    35.     Next
    36.     sw.Stop()
    37.     'Result: ~45021390 on dual core
    38.     Console.WriteLine(sw.ElapsedTicks)
    39.  
    40. End Sub

    Can't think of a more efficient way for either.

  11. #11
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Quicker way of doing this ...?

    vb.net Code:
    1. Dim i As Integer
    2. Dim l As New List(Of String)
    3. For i = 0 To 999
    4. If Not IO.Directory.Exists("C:\MyDir\" & i.ToString() & "A") Then l.Add(i.ToString() & "A")
    5. Next

    l.ToArray() returns an array of strings. Add this to your ListBox. (ListBox1.AddRange(l.ToArray()))

  12. #12

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    73

    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

  13. #13
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    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())

  14. #14

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    73

    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.

  15. #15
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    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

  16. #16
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: Quicker way of doing this ...?

    The way I was talking about is this:

    vb.net Code:
    1. Dim missing As New List(Of String)
    2.         Dim ary() As String = System.IO.Directory.GetDirectories("C:\Windows\")
    3.         Dim str As String = ""
    4.  
    5.         For x As Integer = 0 To 999
    6.             Select Case x
    7.                 Case Is < 100
    8.                     str = "0" & x.ToString & "A"
    9.                 Case Is < 10
    10.                     str = "00" & x.ToString & "A"
    11.                 Case Else
    12.                     str = x.ToString & "A"
    13.             End Select
    14.  
    15.             If Not ary.Contains(str) Then missing.Add(str)
    16.         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
  •  



Featured


Click Here to Expand Forum to Full Width