Solved

Path Spliting and Sorting

Posted on 2004-08-28
14
171 Views
Last Modified: 2010-05-02
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
Comment
Question by:xizor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11920581
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
 
LVL 2

Author Comment

by:xizor
ID: 11920615
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
 
LVL 2

Author Comment

by:xizor
ID: 11920621
Ignore that, found a error...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:xizor
ID: 11920641
Sorry, ignore that first error, i made a mistake...

Now...

It adds the parent node, but thats all...
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11920647
Set LineStyle property of TreeView to 1-tvwRootLines, and try again            
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11920662
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
 
LVL 2

Author Comment

by:xizor
ID: 11920676
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11920707
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
 
LVL 8

Accepted Solution

by:
mladenovicz earned 500 total points
ID: 11920720
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
 
LVL 2

Author Comment

by:xizor
ID: 11920728
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
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11920744
Try with the latest code I posted. It should work
0
 
LVL 2

Author Comment

by:xizor
ID: 11920747
Wow...Your Quick....
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11920749
;)
0
 
LVL 2

Author Comment

by:xizor
ID: 11920757
AWESOME!! You are a LEGEND!!!
Thanks heaps!
`Andy
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question