Tellen van alle folders en subfolders plus grootte

Status
Niet open voor verdere reacties.

bonnowagt

Gebruiker
Lid geworden
7 dec 2006
Berichten
445
Hallo,

Ik heb bijvoorbeeld een hoofddirectoprie in c:\Audio en Video
daaronder weer een tweetal folders en onder de folders weer vele subfolders:

Audio en Video
Muziek
ABBa
Queen
ENZ

Film
Komedie
Enz

Ik heb overal gezoch en genoeg gelezen maa rkom er niet uit. Wat iik wil is dat ik de hoofdfolders van C:\ inlaad. Dat is geen probleem bijvoorbeeld in een listview. Als ik dan bijvoorbeeld op Audio en Video klik ik dan de aantal folders krijg die het bevat dus alle onderliggende folders en de totale aantal grootte van alle folders
Dus bijvoorbeeld Audio en Viedo bevat 163 folders ter grootte van 364 MB
Ik gebruik dus VB 2010
 
Misschien heb je hier wat aan:

Code:
    ''' <summary>Data units to get.</summary>
    Private Enum UnitType
        ''' <summary>Returns the size in bytes.</summary>    
        Bytes
        ''' <summary>Returns the size in kilobytes.</summary>
        Kilobytes
        ''' <summary>Returns the size in megabytes.</summary>
        Megabytes
        ''' <summary>Returns the size in giabytes.</summary>
        Gigabytes
        ''' <summary>Returns the size based on the file that is being passed in.</summary>
        Auto
    End Enum

    ''' <summary>Returns the filesize of a specified file.</summary>
    ''' <param name="sFile">The file to get the filesize of.</param>
    ''' <param name="Units">The data unit to use (GB, MB, KB or Bytes).</param>
    ''' <returns>Returns a string containing the filesize.</returns>
    Private Function GetFileSize(ByVal sFile As String, Optional ByVal Units As UnitType = UnitType.Auto) As String

        Dim FileLength As New FileInfo(sFile)
        Dim Bytes As Long = FileLength.Length
        Return FormatSize(Bytes, Units)

    End Function

    ''' <summary>Returns the filesize of a specified directory.</summary>
    ''' <param name="sPath ">The directory to get the filesize of.</param>
    ''' <param name="Units">The data unit to use (GB, MB, KB or Bytes).</param>
    ''' <returns>Returns a string containing the filesize.</returns>
    Private Function GetDirectorySize(ByVal sPath As String, Optional ByVal Units As UnitType = UnitType.Auto) As String
    
    Dim Dir As New DirectoryInfo(sPath)
        Dim Bytes As Long

        For Each File As FileInfo In Dir.GetFiles("*.*", SearchOption.AllDirectories)
            Bytes += File.Length
        Next
        Return FormatSize(Bytes, Units)

    End Function

    Private Shared Function FormatSize(ByVal Bytes As Long, Optional ByVal Units As UnitType = UnitType.Auto) As String
        
        Select Case Units
            Case 0
                Return Fix(Bytes) & " Bytes"
            Case 1
                Return Format(Bytes / 1024, "#0.00") & " KB"
            Case 2
                Return Format(Bytes / 1024 / 1024, "#0.00") & " MB"
            Case 3
                Return Format(Bytes / 1024 / 1024 / 1024, "#0.00") & " GB"
            Case Else
                If Bytes >= 1073741824 Then
                    Return Format(Bytes / 1024 / 1024 / 1024, "#0.00") & " GB"
                ElseIf Bytes >= 1048576 Then
                    Return Format(Bytes / 1024 / 1024, "#0.00") & " MB"
                ElseIf Bytes >= 1024 Then
                    Return Format(Bytes / 1024, "#0.00") & " KB"
                ElseIf Bytes > 0 And Bytes < 1024 Then
                    Return Fix(Bytes) & " Bytes"
                Else
                    Return "0 Bytes"
                End If
        End Select

    End Function

Dit zijn 3 methods die je kan gebruiken om de grootte van een directory te verkrijgen. Het ziet er misschien wat ingewikkeld uit, als je een voorbeeldje wilt zien, zeg je het maar. :)
 
Laatst bewerkt:
Ja je zou mij er een enorm plezier mee doen. Al is het maar om van te leren. Bedankt alvast
 
Het werkt prima.
Ik heb nude grootte van een totale directory en hoeveeel files. Ben nu nog aanhet puzzelen hoe ik ook het aantal onderliggende mappen kan krijgen.
Maar tot zover werkt het grandioos waarvoor mijn hartelijke dank
 
Zoiets dus:

[cpp]Sub RecursiveSearch(ByRef strDirectory As String, ByRef files As ArrayList, ByRef folders As ArrayList)
Dim dirInfo As New IO.DirectoryInfo(strDirectory)
Dim pFileInfo() As IO.FileInfo = dirInfo.GetFiles()
files.AddRange(pFileInfo)
Dim pdirInfo() As IO.DirectoryInfo
pdirInfo = dirInfo.GetDirectories()
Dim dirIter As IO.DirectoryInfo
For Each dirIter In pdirInfo
folders.Add(dirIter.FullName)
RecursiveSearch(dirIter.FullName, files, folders)
Next dirIter
End Sub[/cpp]

Aanroepen:

[cpp]Dim files, folders As New ArrayList
RecursiveSearch("C:\Test\Test",files,folders)[/cpp]
 
Bedankt voor je snelle reactie maar had net een snellere weg gevonden, gewoon dit er tussen plaatsen boven de files
Dim Dir As New DirectoryInfo(sPath)
Dim Bytes As Long
For Each directory As DirectoryInfo In Dir.GetDirectories("*.*", SearchOption.AllDirectories)
aantalfolders = aantalfolders + 1
Next
 
OK, ik dacht dat je alle namen ook moest hebben ;)

Zet je de vraag op Opgelost?
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan