Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

Path Spliting and Sorting

I've a search routine that searches the hard drives and find some particular files whereever they are located.

I have them stored in list1
A sample list would be:
"c:\fileabc.typ"
"c:\program files\Visual Basic\file huh.typ"
"c:\program files\Visual Basic\Common\Filehuh2.typ"
"c:\windows\fileabb.typ"
"c:\windows\system\file.typ"
"f:\data\docs\jokes\filea.typ"
"f:\data\docs\jokes\filedd.typ"
"f:\data\docs\jokes\filec.typ"
"f:\data\docs\acconts\fileb.typ"
"f:\data\docs\acconts\filec.typ"

I want to take the list, split up the paths and display the found files as a treeview
Results of sample data would be:

C:
--fileabc.typ
--program files
------Visual Basic
----------file huh.typ
------common
----------filehuh2.typ
--windows
------fileabb.typ
------system
----------fileb.typ
f:
--data
----docs
------accounts
----------fileb.typ
----------filec.typ
------jokes
---------filea.typ
---------filedd.typ
---------filec.typ


I hope this makes sence.

Cheers,

`Andy
0
xizor
Asked:
xizor
  • 7
  • 7
1 Solution
 
mladenoviczCommented:
Private Sub Command1_Click()
Dim Nd As Node
Dim ParentNd As Node
Dim ArrPaths

For i = 0 To List1.ListCount - 1
    ArrPaths = Split(List1.List(i), "\")
    For j = LBound(ArrPaths) To UBound(ArrPaths)
        Set Nd = SafeGetNode(ArrPaths(j))
        If Nd Is Nothing Then
            If ParentNd Is Nothing Then
                Set ParentNd = TreeView1.Nodes.Add(, tvwChild, ArrPaths(j), ArrPaths(j))
            Else
                Set ParentNd = TreeView1.Nodes.Add(ParentNd, tvwChild, ArrPaths(j), ArrPaths(j))
            End If
        Else
            Set ParentNd = Nd
        End If
    Next
    Set ParentNd = Nothing
Next

End Sub

Public Function SafeGetNode(Ind As Variant) As Node
    On Error Resume Next
    Set SafeGetNode = TreeView1.Nodes.Item(Ind)
End Function
0
 
xizorAuthor Commented:
I haven't ever used the treeview so i'm at a loss...

Error on Set ParentNd...

            If ParentNd Is Nothing Then
                Set ParentNd = TreeView1.Nodes.Add(, tvwChild, ArrPaths(J), ArrPaths(J))
            Else

Error: "Key is not unique in collection"

Any Ideas?

`Andy
0
 
xizorAuthor Commented:
Ignore that, found a error...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
xizorAuthor Commented:
Sorry, ignore that first error, i made a mistake...

Now...

It adds the parent node, but thats all...
0
 
mladenoviczCommented:
Set LineStyle property of TreeView to 1-tvwRootLines, and try again            
0
 
mladenoviczCommented:
Als, if you want nodes to be sorted, you should use Sorted property

Private Sub Command1_Click()
Dim Nd As Node
Dim ParentNd As Node
Dim ArrPaths

    For i = 0 To List1.ListCount - 1
        ArrPaths = Split(List1.List(i), "\")
        For j = LBound(ArrPaths) To UBound(ArrPaths)
            Set Nd = SafeGetNode(ArrPaths(j))
            If Nd Is Nothing Then
                If ParentNd Is Nothing Then
                    Set ParentNd = TreeView1.Nodes.Add(, tvwChild, ArrPaths(j), ArrPaths(j))
                Else
                    Set ParentNd = TreeView1.Nodes.Add(ParentNd, tvwChild, ArrPaths(j), ArrPaths(j))
                End If
                ParentNd.Sorted = True
            Else
                Set ParentNd = Nd
            End If
        Next
        Set ParentNd = Nothing
    Next
   
    TreeView1.Sorted = True
End Sub


0
 
xizorAuthor Commented:
ah....

the root item now has a + next to it... it wasn't expanding.

Is there a way to expand the entire list when its done?

Its not quite working... not processing everything... not all files and folders are being listed...
0
 
mladenoviczCommented:
Use Expanded property to expand node

Private Sub Command1_Click()
Dim Nd As Node
Dim ParentNd As Node
Dim ArrPaths

    For i = 0 To List1.ListCount - 1
        ArrPaths = Split(List1.List(i), "\")
        For j = LBound(ArrPaths) To UBound(ArrPaths)
            Set Nd = SafeGetNode(ArrPaths(j))
            If Nd Is Nothing Then
                If ParentNd Is Nothing Then
                    Set ParentNd = TreeView1.Nodes.Add(, tvwChild, ArrPaths(j), ArrPaths(j))
                Else
                    Set ParentNd = TreeView1.Nodes.Add(ParentNd, tvwChild, ArrPaths(j), ArrPaths(j))
                End If
                ParentNd.Sorted = True
                ParentNd.Expanded = True
            Else
                Set ParentNd = Nd
            End If
        Next
        Set ParentNd = Nothing
    Next
   
    TreeView1.Sorted = True
End Sub

This code doesn't display all items because there are files with a same name, but name is used as a key. I will try to fix that
0
 
mladenoviczCommented:
This should work

Private Sub Command1_Click()
Dim Nd          As Node
Dim ParentNd    As Node
Dim ArrPaths()  As String
Dim j           As Integer
Dim i           As Integer

    For i = 0 To List1.ListCount - 1
        ArrPaths = Split(List1.List(i), "\")
        For j = LBound(ArrPaths) To UBound(ArrPaths)
            Set Nd = SafeGetNode(MakeKey(ArrPaths(), j))
            If Nd Is Nothing Then
                If ParentNd Is Nothing Then
                    Set ParentNd = TreeView1.Nodes.Add(, tvwChild, MakeKey(ArrPaths, j), ArrPaths(j))
                Else
                    Set ParentNd = TreeView1.Nodes.Add(ParentNd, tvwChild, MakeKey(ArrPaths, j), ArrPaths(j))
                End If
                ParentNd.Sorted = True
                ParentNd.Expanded = True
            Else
                Set ParentNd = Nd
            End If
        Next
        Set ParentNd = Nothing
    Next
   
    TreeView1.Sorted = True
End Sub

Public Function SafeGetNode(Ind As Variant) As Node
    On Error Resume Next
    Set SafeGetNode = TreeView1.Nodes.Item(Ind)
End Function

Private Sub Form_Load()
List1.AddItem "c:\fileabc.typ"
List1.AddItem "c:\program files\Visual Basic\file huh.typ"
List1.AddItem "c:\program files\Visual Basic\Common\Filehuh2.typ"
List1.AddItem "c:\windows\fileabb.typ"
List1.AddItem "c:\windows\system\file.typ"
List1.AddItem "f:\data\docs\jokes\filea.typ"
List1.AddItem "f:\data\docs\jokes\filedd.typ"
List1.AddItem "f:\data\docs\jokes\filec.typ"
List1.AddItem "f:\data\docs\acconts\fileb.typ"
List1.AddItem "f:\data\docs\acconts\filec.typ"

End Sub


Public Function MakeKey(Arr() As String, CurrItem As Integer) As String
    Dim res As String
    For i = LBound(Arr) To CurrItem
        If res <> "" Then
            res = res & "\" & Arr(i)
        Else
            res = Arr(i)
        End If
    Next
    MakeKey = res
End Function
0
 
xizorAuthor Commented:
Not all the files and folders are getting listed. I VERY WELL MAY BE WRONG, BUT I think this is because some names are duplicates:

Sample Data:
F:\Data\User1\Accounts
F:\Data\User1\Work\Project1\Other
F:\Data\User1\Work\Project1\More\Again
F:\Data\User1\Misc
F:\Data\User2\Accounts
F:\Data\User2\Work\Project1\Other
F:\Data\User2\My Documents
F:\Data\User3\Accounts
F:\Data\User3\Work\Project1\Other
F:\Data\User3\Misc

In this sample data, the treeview is filled out for User1 completely.
User2 and User3 get listed, but none of their sub items are listed.

Cheers,

`Andy
0
 
mladenoviczCommented:
Try with the latest code I posted. It should work
0
 
xizorAuthor Commented:
Wow...Your Quick....
0
 
mladenoviczCommented:
;)
0
 
xizorAuthor Commented:
AWESOME!! You are a LEGEND!!!
Thanks heaps!
`Andy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now