|
-
May 8th, 2011, 12:04 AM
#1
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:
Private Sub filltree()
Dim nod As Object, n As Object, initdir As Variant
If Not sh Is Nothing Then Exit Sub
pathsep = "\" 'declare globally
'initdir = "c:\"
If InStr(initdir, "/") > 0 Then pathsep = "/"
Set sh = CreateObject("shell.application")
Set n = sh.namespace(initdir)
If Not Right(initdir, 1) = pathsep Then initdir = initdir & pathsep
Set nod = tv.Nodes.Add(, , initdir, n.self.Name)
listdirs n, nod
nod.Expanded = True
End Sub
Sub listdirs(f As Object, nod As Object)
Dim nod2 As Object, fitem As Object, i As Integer
If f Is Nothing Then Exit Sub
For Each fitem In f.items
If fitem.isfolder Then
Set nod2 = tv.Nodes.Add(nod, tvwChild, nod.Key & fitem.Name & pathsep, fitem.Name)
' listdirs sh.namespace(fitem), nod2
End If
DoEvents
Next
End Sub
Private Sub tv_NodeClick(ByVal Node As MSComctlLib.Node)
If Node.Children > 0 Then Node.Expanded = True: Exit Sub
listdirs sh.namespace(Node.Key), Node
Node.Expanded = True
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|