Solved

Path Spliting and Sorting

Posted on 2004-08-28
14
169 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
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
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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

830 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