Get file size from folder by filter
I’m using vb.net.
I wanna ask how I can get the file size for each file by filter?
I wanna get all the size of .ts files
here’s the code I’m using
Dim TotalSize As Long = 0
Sub FileSize()
Dim TheSize As Long = GetDirSize(txtPath.Text)
TotalSize = 0 'Reset the counter
If TheSize < 1024 Then
lblSize.Text = Math.Round(TheSize, 0) & " B"
ElseIf TheSize > 1024 AndAlso TheSize < (1024 ^ 2) Then
lblSize.Text = Math.Round(TheSize / 1024, 1) & " KB"
ElseIf TheSize > (1024 ^ 2) AndAlso TheSize < (1024 ^ 3) Then
lblSize.Text = Math.Round(TheSize / 1024 / 1024, 1) & " MB"
ElseIf TheSize > (1024 ^ 3) AndAlso TheSize < (1024 ^ 4) Then
lblSize.Text = Math.Round(TheSize / 1024 / 1024 / 1024, 1) & " GB"
End If
End Sub
Public Function GetDirSize(folder As String) As Long
Dim FolderInfo = New DirectoryInfo(folder)
For Each File In FolderInfo.GetFiles : TotalSize += File.Length
Next
For Each SubFolderInfo In FolderInfo.GetDirectories : GetDirSize(SubFolderInfo.FullName)
Next
Return TotalSize
End Function
Go to Source
Author: zackmark29