Results 1 to 12 of 12

Thread: fill directory structure into treeview from local or ftp

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    fill directory structure into treeview from local or ftp

    this developed from a thread in vb6,
    http://www.vbforums.com/showthread.php?t=649217
    and i was pleased enough with the results i decided to move it to code bank

    any recursive directory search is slow, even on the local machine, as it has to test every file to see if it is a folder

    i am sure there would be better ways to do it, you only have to look at the way firefox ftp addin does it

    to improve speed, it should be possible to only populate the root, then populate each of the nodes as they are opened, here is modified code, also solves unique key issue

    for those who did not see the original thread
    both sh as object and pathsep as string, need to be declared globally for the code to work correctly, feel free to change this if you do not like using global variables
    vb Code:
    1. Private Sub filltree()
    2.       Dim nod As Object, n As Object, initdir As Variant
    3.       If Not sh Is Nothing Then Exit Sub
    4.       pathsep = "\"  'declare globally
    5.       initdir = "ftp://username:[email protected]"
    6.       'initdir = "c:\"
    7.       If InStr(initdir, "/") > 0 Then pathsep = "/"
    8.       Set sh = CreateObject("shell.application")
    9.       Set n = sh.namespace(initdir)
    10.       If Not Right(initdir, 1) = pathsep Then initdir = initdir & pathsep
    11.       Set nod = tv.Nodes.Add(, , initdir, n.self.Name)
    12.       listdirs n, nod
    13.       nod.Expanded = True
    14.       End Sub
    15.  
    16.       Sub listdirs(f As Object, nod As Object)
    17.       Dim nod2 As Object, fitem As Object, i As Integer
    18.       If f Is Nothing Then Exit Sub
    19.       For Each fitem In f.items
    20.           If fitem.isfolder Then
    21.               Set nod2 = tv.Nodes.Add(nod, tvwChild, nod.Key & fitem.Name & pathsep, fitem.Name)
    22.       '        listdirs sh.namespace(fitem), nod2
    23.           End If
    24.           DoEvents
    25.       Next
    26.       End Sub
    27.        
    28.       Private Sub tv_NodeClick(ByVal Node As MSComctlLib.Node)
    29.       If Node.Children > 0 Then Node.Expanded = True: Exit Sub
    30.       listdirs sh.namespace(Node.Key), Node
    31.       Node.Expanded = True
    32.       End Sub

    the advantage here to using a shell object is you can use for either local of FTP folders and can start the tree from any level as supplied in initdir

    as you might note i did this using late binding, you can add a reference to shell controls and automation, then change your variables to appropriate types, no other APIs or references are required, except treeview (tv) change name to suit

    change ftp host, username and password as required

    i tested on local harddrive runs seemlessly, pretty bloody good on ftp, even with very slow connection
    i did some editing after pasting, so hope no errors

    i am certain that similar code can be used with the ftp APIs to produce identical results for ftp, if desired
    Last edited by westconn1; May 9th, 2011 at 04:58 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width