Solved

Path Spliting and Sorting

Posted on 2004-08-28
14
167 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
  • 7
  • 7
14 Comments
 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
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
Comment Utility
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
Comment Utility
Ignore that, found a error...
0
 
LVL 2

Author Comment

by:xizor
Comment Utility
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
Comment Utility
Set LineStyle property of TreeView to 1-tvwRootLines, and try again            
0
 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Try with the latest code I posted. It should work
0
 
LVL 2

Author Comment

by:xizor
Comment Utility
Wow...Your Quick....
0
 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
;)
0
 
LVL 2

Author Comment

by:xizor
Comment Utility
AWESOME!! You are a LEGEND!!!
Thanks heaps!
`Andy
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now