Question Directory/Folder/File Searching recursive?

gvjonnyv

Member
Joined
Jan 15, 2009
Messages
7
Programming Experience
Beginner
I am extremely new to programming and need some serious help. I took some VB classes in high school, some Java in college and have messed around with macros, that is my extent of previous knowledge. Long story short, I have an internship where the learning involved is mostly self taught. I've been trying to learn VB through Google searches, forums, etc.

Right now I'm trying to make a form that displays folders and files based on a given size. I browse to a certain folder or directory and it displays all of the folders and files that are greater than the size that I specify. However, I can not get farther than one level of sub-folder down.

I've done a lot of searching to see how to get all of the sub-folders and there are A LOT of answers and code out there. Every time I try to use other's ideas along with my code I get too confused to manipulate it into my design. I see a lot of answers using a recursive function, but need someone to help explain it to me otherwise it all just looks like random babble to me.

Here is my code that returns one level of subfolder and files.

VB.NET:
Private Sub BrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseButton.Click
        FolderBrowserDialog1.ShowDialog()
        textLocation.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        Dim objDir As DirectoryInfo = New DirectoryInfo(textLocation.Text)
        Dim objFolder As DirectoryInfo
        Dim subFolder As DirectoryInfo
        Dim file As FileInfo
        Dim dblTotalSize As Double
        Dim objArray As New SortedList
        Dim strToList As String
        Dim strCCList As String = "Done."
        Try
            Me.Text = "Please wait..."
            For Each objFolder In objDir.GetDirectories()
                dblTotalSize = GetFolderSize(objFolder.FullName, True) / 1048576
                If dblTotalSize > NumericUpDown1.Value Then
                    strToList += "Folder Name:  " + objFolder.FullName + " (" + CStr(Round(dblTotalSize, 2)) + " MB)" & vbCrLf
                    For Each file In objFolder.GetFiles()
                        Dim fileSize As Double
                        fileSize = (file.Length) / 1048576
                        If fileSize > NumericUpDown2.Value Then
                            strToList += "          File Name:  " + file.Name + " (" + CStr(Round(fileSize, 2)) + " MB)" & vbCrLf
                        End If
                    Next
                End If
                For Each subFolder In objFolder.GetDirectories()
                    dblTotalSize = GetFolderSize(subFolder.FullName, True) / 1048576
                    If dblTotalSize > NumericUpDown1.Value Then
                        strToList += "     Sub-Folder Name:  " + subFolder.Name + " (" + CStr(Round(dblTotalSize, 2)) + " MB)" & vbCrLf
                    End If
                    For Each file In subFolder.GetFiles
                        Dim fileSize As Double
                        fileSize = (file.Length) / 1048576
                        If fileSize > NumericUpDown2.Value Then
                            strToList += "          File Name:  " + file.Name + " (" + CStr(Round(fileSize, 2)) + " MB)" & vbCrLf
                        End If
                    Next
                Next
            Next
            txtOutput.Text += strToList & vbCrLf & vbCrLf & strCCList & vbCrLf & vbCrLf
            Me.Text = "Folder Size - Done!"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Function GetFolderSize(ByVal DirPath As String, Optional ByVal IncludeSubFolders As Boolean = True) As Long
        Dim lngFolderSize As Long
        Dim objFileInfo As FileInfo
        Dim objFolder As DirectoryInfo
        Dim objSubFolder As DirectoryInfo
        Try
            objFolder = New DirectoryInfo(DirPath)
            For Each objFileInfo In objFolder.GetFiles()
                lngFolderSize += objFileInfo.Length
            Next
            If IncludeSubFolders Then
                For Each objSubFolder In objFolder.GetDirectories()
                    lngFolderSize += GetFolderSize(objSubFolder.FullName)
                Next
            End If
        Catch Ex As Exception
            MsgBox(Ex.Message)
        End Try
        Return lngFolderSize
    End Function

    Function Round(ByVal nValue As Double, ByVal nDigits As _
Integer) As Double
        Round = Int(nValue * (10 ^ nDigits) + _
        0.5) / (10 ^ nDigits)
    End Function

    Private Sub CopyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyButton.Click
        Clipboard.SetDataObject(txtOutput.Text.ToString)
    End Sub

If anyone can offer any type of help in lamens terms for my newbie self, I would greatly appreciate it.

Thanks
 
I am not sure this will help. I think I have found a shorter example here. Anyway add the large enough files/directories to an array or stack. There is a file size which you would need t call before the loop here which can be translated here.Good Luck
 
Last edited:
Back
Top