Splitting Data Folder and Add In TreeView

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 1877 Views - Last Post: 19 February 2016 - 05:08 PM Rate Topic: -----

#1 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 26-January 15

Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 03:15 AM

Hi, I need to add in a treeview the structure of a directory (subfolders and files) after a search of all files on the inside, splitting the folder in by size chosen.

I Used this code to split a directory in size and only add the folders and files split into a treeview for each cycle.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Me.TreeView1.Nodes.Clear()

        Dim Source As String = "C:\TEST"
        Dim Source_Dir As New DirectoryInfo(Source)

        Dim Split_Size As Long = 0

        For Each Files As FileInfo In Source_Dir.EnumerateFiles("*", SearchOption.AllDirectories)

            If (Split_Size.ToString / 1048576) >= 20 Then ' MBytes  

                Split_Size = 0 ' Reset 
                MsgBox("New Split Data : " & Split_Data.Text)
                TreeView1.Nodes.Clear()

            ElseIf (Split_Size.ToString / 1048576) < 20 Then

                TreeView1.BeginUpdate()
                TreeView1.Nodes.Add(Files.FullName)
                TreeView1.EndUpdate()
                TreeView1.Refresh()

            End If
            Split_Size += New FileInfo(Files.FullName).Length
            Split_Data.Text = (Split_Size \ 1048576).ToString("N0") & " °MB"
        Next
        MsgBox("Last Split Data : " & Split_Data.Text)

    End Sub



In the example I have a folder of 80mb I split the folder in 20mb in each cycle I add the parent node and related child nodes in treeview with the files, but I find myself in such a situation.

Posted Image :no:/>

I would like a situation like this, like the classic method to add in a treeview the structure of an entire folder.. In order to read and open the files or folders split, directly from treeview... :smile2:/>

Posted Image

I tried in every way but I can not figure out how to create the structure of the treeview with inside the parent folder, including subfolders and files for each cycle of splitted folder.
I'm hoping for some help.. :surrender:/>

Is This A Good Question/Topic? 0
  • +

Replies To: Splitting Data Folder and Add In TreeView

#2 TechnoBear  Icon User is offline

  • Lady A
  • member icon

Reputation: 185
  • View blog
  • Posts: 818
  • Joined: 02-November 11

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 05:00 AM

Pretty sure something like this would work:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Get a list of drives
        Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
        Dim rootDir As String = String.Empty
        'Now loop thru each drive and populate the treeview
        For i As Integer = 0 To drives.Count - 1
            rootDir = drives(i).Name
            'Add this drive as a root node
            TreeView1.Nodes.Add(rootDir)
            'Populate this root node
            PopulateTreeView(rootDir, TreeView1.Nodes(i))
        Next
    End Sub
 
    'This is a recursive function - it should populate your tree view just fine
    Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
        Dim folder As String = String.Empty
        Try
            Dim folders() As String = IO.Directory.GetDirectories(dir)
            If folders.Length <> 0 Then
                Dim childNode As TreeNode = Nothing
                For Each folder In folders
                    childNode = New TreeNode(folder)
                    parentNode.Nodes.Add(childNode)
                    PopulateTreeView(folder, childNode)
                Next
            End If
        Catch ex As UnauthorizedAccessException
            parentNode.Nodes.Add(folder & ": Access Denied")
        End Try
    End Sub


I just happen to be working on something that uses something like the above at the moment
Was This Post Helpful? 1
  • +
  • -

#3 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,239
  • Joined: 07-September 13

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 05:20 AM

Or another way to do it, and i am more or less posting this as i have now written it:

However TechnoBear's will work just as well.


Public Class Form1

    Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


        Me.TreeView1.Nodes.Clear()


        ' So i have changed the source to point at c:\ProgramFiles (x86) so the first node will say that but afterwards
        ' the folders and nodes will be seperaed. plus it give you something to play around with a solve.
        Dim Source As String = "C:\"

        ' Ok so this is me creating a treenode for the source treenode
        Dim sourceTreeNode As New TreeNode(Source)

        ' Now i have seperated out our recursive routines into two, 
        ' the first will enumerate the directories for a give path(source)
        ' the second will enumerate the files for a given path(source)

        ' Take note i am also passing through the sourceTreeNode, this is so we can keep building this base node up 
        ' by adding directories and files to it, either directly or via children.
        Try
            EnumerateDirectories(Source, sourceTreeNode)
            EnumerateFiles(Source, sourceTreeNode)
        Catch ex As UnauthorizedAccessException
            sourceTreeNode.Text += " - Unauthorised"
        End Try

        ' now that we can enumerate the path we will add our source treenode to the treeview.
        TreeView1.Nodes.Add(sourceTreeNode)

        ' please note this example does take about 1-3 seconds going through MY 'c:\Program Files(x86)'
        ' so it isn't the quickest in the world and there is a number of things you can do to improve it. 
        ' however this example is only meant to help you and not to give you a full example.

    End Sub

    Private Sub EnumerateDirectories(path As String, parentDirectoryTreeNode As TreeNode)

        If Not System.IO.Directory.Exists(path) Then Exit Sub

        ' Next we will enumerate through the directory, looking at the code you had it does the same thing, it turns a array of strings
        ' therefore i am just doing the same thing but with a function i have used before.
        For Each directoryPath As String In System.IO.Directory.GetDirectories(path)
            ' Getting the information for the directory
            Dim dirInfo As New System.IO.DirectoryInfo(directoryPath)
            ' Creating a tree node for the directory
            Dim dirTreeNode As New TreeNode(dirInfo.Name)

            Try
                ' Enumerate the directories for this folder 
                EnumerateDirectories(directoryPath, dirTreeNode)
                ' enumerate the files for this folder
                EnumerateFiles(directoryPath, dirTreeNode)
            Catch ex As UnauthorizedAccessException
                dirTreeNode.Text += " - Unauthorised"
            End Try

            ' Then finally add the node to the passed in node
            parentDirectoryTreeNode.Nodes.Add(dirTreeNode)
        Next

    End Sub

    Private Sub EnumerateFiles(path As String, parentDirectoryTreeNode As TreeNode)

        If Not System.IO.Directory.Exists(path) Then Exit Sub

        ' Next we will enumerate through the files, looking at the code you had it does the same thing, it turns a array of strings
        ' therefore i am just doing the same thing but with a function i have used before.
        For Each filePath As String In System.IO.Directory.GetFiles(path)
            ' Getting the information for the directory
            Dim fInfo As New System.IO.FileInfo(filePath)
            ' adding the treenode as it can go any further down.
            parentDirectoryTreeNode.Nodes.Add(fInfo.Name)
        Next

    End Sub

End Class



Updated the code

This post has been edited by maceysoftware: 18 February 2016 - 05:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,239
  • Joined: 07-September 13

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 05:52 AM

Or you could do it this way:

There really is alot of ways you can do this.

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        TreeView1.Nodes.Clear()
 
        TreeView1.SuspendLayout()

        For Each drive As DriveInfo In My.Computer.FileSystem.Drives.Where(Function(d) d.DriveType = DriveType.Fixed)

            Dim sourceTreeNode As New TreeNode(drive.Name)

            Try
                EnumerateDirectories(drive.Name, sourceTreeNode)
                EnumerateFiles(drive.Name, sourceTreeNode)
            Catch ex As UnauthorizedAccessException
                sourceTreeNode.Text += " - Unauthorised"
            End Try
            ' now that we can enumerate the path we will add our source treenode to the treeview.
            TreeView1.Nodes.Add(sourceTreeNode)
        Next

        TreeView1.ResumeLayout()

    End Sub

    Private Sub EnumerateDirectories(path As String, parentDirectoryTreeNode As TreeNode)

        If Not System.IO.Directory.Exists(path) Then Exit Sub

        Array.ForEach(System.IO.Directory.GetDirectories(path), Sub(directory)

                                                                    ' sadly you have to use getFileName for this as GetDirectoryName will get the name of the directory above
                                                                    Dim dirTreeNode As New TreeNode(System.IO.Path.GetFileName(directory))

                                                                    Try
                                                                        ' Enumerate the directories for this folder 
                                                                        EnumerateDirectories(directory, dirTreeNode)
                                                                        ' enumerate the files for this folder
                                                                        EnumerateFiles(directory, dirTreeNode)
                                                                    Catch ex As UnauthorizedAccessException
                                                                        dirTreeNode.Text += " - Unauthorised"
                                                                    End Try
                                                                    parentDirectoryTreeNode.Nodes.Add(dirTreeNode)

                                                                End Sub)


    End Sub

    Private Sub EnumerateFiles(path As String, parentDirectoryTreeNode As TreeNode)

        If Not System.IO.Directory.Exists(path) Then Exit Sub

        Array.ForEach(System.IO.Directory.GetFiles(path), Sub(file) parentDirectoryTreeNode.Nodes.Add(System.IO.Path.GetFileName(file)))

    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#5 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 26-January 15

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 07:23 AM

Thanks guys for examples, but these are the classic methods to display a whole structure of a folder within a treeview.. :plain:/>/>

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim folderPath = "C:\TEST"

        Dim RootFolderInfor As DirectoryInfo = New DirectoryInfo(folderPath)

        Dim treeNode As TreeNode = New TreeNode(RootFolderInfor.FullName)

        Me.TreeView1.SuspendLayout()

        Me.TreeView1.Nodes.Add(treeNode)

        AddToTreeview(treeNode, folderPath)

        Me.TreeView1.ResumeLayout()

    End Sub
    Private Sub AddToTreeview(ByVal RootNode As TreeNode, ByVal path As String)

        Dim dicInfor As DirectoryInfo = New DirectoryInfo(path)

        For Each FInfor As FileInfo In dicInfor.GetFiles()

            'Add all the files to treeview.

            RootNode.Nodes.Add(FInfor.Name)

        Next

        For Each DInfo As DirectoryInfo In dicInfor.GetDirectories()

            'add the folder and its decendents to treeview.

            Dim NewNode As TreeNode = New TreeNode(DInfo.Name)

            RootNode.Nodes.Add(NewNode)

            AddToTreeview(NewNode, DInfo.FullName)

        Next

    End Sub



I need to be added only subfolders and files to the root node ("C: \ TEST") when searching for the variable (Files), then create only the structure scanned from:
For Each Files As FileInfo In Source_Dir.EnumerateFiles("*", SearchOption.AllDirectories)


I get this by inserting
TreeView1.Nodes.Add(Files.FullName)


Posted Image

I would like to get this but with my code to split folders..

Posted Image

How do I make work my code together with the classic examples posted to display correctly only the files and folders read from the variable (Files) in the treeview.. :sleepy:/>/>
Was This Post Helpful? 0
  • +
  • -

#6 TechnoBear  Icon User is offline

  • Lady A
  • member icon

Reputation: 185
  • View blog
  • Posts: 818
  • Joined: 02-November 11

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 07:39 AM

just add a check for Files into my Folder checker thing... or either of Macey's...
Was This Post Helpful? 0
  • +
  • -

#7 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 26-January 15

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 10:04 AM

View PostTechnoBear, on 18 February 2016 - 07:39 AM, said:

just add a check for Files into my Folder checker thing... or either of Macey's...

I'm sorry but I've tried everything but it does not work, you may give an example using my code that I posted to split a folder with a method to display correctly the treeview.
All I ask is five minutes of your time I would thank you very much.. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#8 TechnoBear  Icon User is offline

  • Lady A
  • member icon

Reputation: 185
  • View blog
  • Posts: 818
  • Joined: 02-November 11

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 10:19 AM

Show me what you have after using the advice already given by me or Macey, with that I can better advise you on how to continue. Otherwise I would be working blind and that won't help either of us.
Was This Post Helpful? 0
  • +
  • -

#9 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,239
  • Joined: 07-September 13

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 10:22 AM

but i gave an example, my first example:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


        Me.TreeView1.Nodes.Clear()


        ' So i have changed the source to point at c:\ProgramFiles (x86) so the first node will say that but afterwards
        ' the folders and nodes will be seperaed. plus it give you something to play around with a solve.
        Dim Source As String = "C:\"

        ' Ok so this is me creating a treenode for the source treenode
        Dim sourceTreeNode As New TreeNode(Source)

        ' Now i have seperated out our recursive routines into two, 
        ' the first will enumerate the directories for a give path(source)
        ' the second will enumerate the files for a given path(source)

        ' Take note i am also passing through the sourceTreeNode, this is so we can keep building this base node up 
        ' by adding directories and files to it, either directly or via children.
        Try
            EnumerateDirectories(Source, sourceTreeNode)
            EnumerateFiles(Source, sourceTreeNode)
        Catch ex As UnauthorizedAccessException
            sourceTreeNode.Text += " - Unauthorised"
        End Try




Source in your case would be c:\test.

as my comment says the first node is the whole text for a node, but i am sure it wouldn't take you long to sort that out.

This post has been edited by maceysoftware: 18 February 2016 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

#10 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 26-January 15

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 10:35 AM

View PostTechnoBear, on 18 February 2016 - 10:19 AM, said:

Show me what you have after using the advice already given by me or Macey, with that I can better advise you on how to continue. Otherwise I would be working blind and that won't help either of us.

I need to include this my function...
For Each Files As FileInfo In Source_Dir.EnumerateFiles("*", SearchOption.AllDirectories)

            If (Split_Size.ToString / 1048576) >= 20 Then ' MBytes  

                Split_Size = 0 ' Reset 
                MsgBox("New Split Data : " & Split_Data.Text)
                TreeView1.Nodes.Clear()

            ElseIf (Split_Size.ToString / 1048576) < 20 Then

                TreeView1.BeginUpdate()
                TreeView1.Nodes.Add(Files.FullName)
                TreeView1.EndUpdate()
                TreeView1.Refresh()

            End If
            Split_Size += New FileInfo(Files.FullName).Length
            Split_Data.Text = (Split_Size \ 1048576).ToString("N0") & " °MB"
        Next


In one codes that you posted so that you get the treeview only the folders and files divided, and not all the contents of the directory.

@maceysoftware OK But where and how to insert the folder split code in your example....

This post has been edited by Dreamfall: 18 February 2016 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#11 TechnoBear  Icon User is offline

  • Lady A
  • member icon

Reputation: 185
  • View blog
  • Posts: 818
  • Joined: 02-November 11

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 12:10 PM

I'm thinking I might just be mentally burnt out from work right now but I cannot make sense of what you mean in this last post. Maybe it'd help if you outlined your requirements a bit clearer.

I just looked at Macey's first post and it definitely adds files and folders, is there a reason that's not acceptable?
Was This Post Helpful? 0
  • +
  • -

#12 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 26-January 15

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 01:42 PM

At each cycle (For ... Next of the code of splitting) I have to add in treeview folders and files splitted by the set size, so only 20mb of folders and files, and not all of 80mb folder as examples you posted.
I already knew the classical method to write treeview in the structure of an entire folder, but I have to split the folder and We add only the files and folders that correspond to the set size, and not the whole folder.

Posted Image

I'm sorry but I'm not a native English speaker, more than as well I can not explain.

This post has been edited by Dreamfall: 18 February 2016 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Build your own boat!
  • member icon

Reputation: 6204
  • View blog
  • Posts: 24,735
  • Joined: 12-December 12

Re: Splitting Data Folder and Add In TreeView

Posted 18 February 2016 - 05:12 PM

I don't follow either. What should happen after the set size has been exceeded? Should all the listed files be removed, but the folders and subfolders be kept? Or should a new (root) node be started, repeating the folder structure that has already been added, but without the earlier listed files?

Quote

I'm sorry but I've tried everything but it does not work, you may give an example using my code that I posted to split a folder with a method to display correctly the treeview.
All I ask is five minutes of your time I would thank you very much..

Bear in mind that we will advise you and provide code samples, but will not edit and write your code for you.
Was This Post Helpful? 0
  • +
  • -

#14 Dreamfall  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 26-January 15

Re: Splitting Data Folder and Add In TreeView

Posted 19 February 2016 - 01:59 AM

Oh Yess...each cycle should be added the new subfolders and files with the size set, in the root directory of the treeview, and so on, clean up the treeview and add the next subfolders and files, until the end of the reading of the entire root folder :smile2:
Was This Post Helpful? 0
  • +
  • -

#15 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,239
  • Joined: 07-September 13

Re: Splitting Data Folder and Add In TreeView

Posted 19 February 2016 - 02:13 AM

Your posts don't follow the same message all the way through, first thing your saying is you want to recursively going through each folder in a folder.
Now your saying 'until the end of the reading of the entire root folder' so you only want to read the root folder?

So basically the treeview will look something like:

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3