To Get the Size of Directory
This code sample explains how to search the Files or to get the count of the number of files in a directory or to get the attributes(date created,Modified,Accessed, etc.,)of the files or to get the size of directory using Findfirstfile and findnextfile. This example is faster compared to the normal function.
Imports System.Runtime.InteropServices
Imports System.IO
#Region "Structure and related object declaration"
'''
''' To check the file size of the folder in quick time
'''
'''
Structure WIN32_FIND_DATA
Dim fileAttributes As Integer
Dim creationTime As Long
Dim lastAccessTime As Long
Dim lastWriteTime As Long
Dim nFileSizeHigh As Double
Dim nFileSizeLow As Double
Dim dwReserved0 As Double
Dim dwReserved1 As Double
Dim fileName As String
Dim alternateFileName As String
End Structure 'WIN32_FIND_DATA
Structure FILE_PARAMS
Dim bRecurse As Boolean
Dim FileCount As Long
Dim FileSize As Decimal '64 bit value
Dim FileSizePresrvhold As Decimal '64 bit value
Dim Searched As Long
Dim sFileNameExt As String
Dim sFileRoot As String
End Structure
Declare Auto Function FindFirstFile Lib "kernel32.dll" _
(
Private Declare Auto Function FindClose Lib "kernel32.dll" _
(ByVal hFindFile As IntPtr) As IntPtr
Declare Auto Function FindNextFile Lib "kernel32.dll" _
(ByVal hFindFile As IntPtr,
ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" (ByVal lpString As Long) As Long
Dim WFD As WIN32_FIND_DATA = New WIN32_FIND_DATA
Private Const vbDot = 46
Private Const MAXDWORD As Long = &HFFFFFFFF
Private Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE As Integer = -1
Private Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10
Dim fp As FILE_PARAMS
Dim Swtocheckfiles As StreamWriter
'''
''' To get the Driectory size of My docs
'''
''' Root path It should be P:\jk\ after the folder need to have a slash at the end
''' of the folder
'''
Private Sub GetDirectorySize(ByVal sRoot As String)
Dim wfd As WIN32_FIND_DATA
Dim hFile As Long
hFile = FindFirstFile(sRoot & "*.*", wfd)
If hFile <> INVALID_HANDLE_VALUE Then
Do
Try
'to refresh the Screen while calculating file count and size
If (wfd.fileName.ToString() <> "." And wfd.fileName.ToString() <> "..") Then
If (wfd.fileAttributes And vbDirectory) Then
GetDirectorySize(sRoot & wfd.fileName.Trim() & "\")
Else
fp.FileCount += 1
End If 'If WFD.dwFileAttributes
End If 'If Asc(wfd.cFileName)
Catch ex As Exception
Continue Do
End Try
Loop While FindNextFile(hFile, wfd)
End If 'If hFile
FindClose(hFile)
'GetDirectorySize = fp.FileSize
End Sub
'''
''' It will return the size in MB
'''
'''
'''
Public Function TogetDirectorySize(ByVal Rootpath As String) As String()
Dim strsize As String
If Rootpath.EndsWith("\") Then
Else
Rootpath = Rootpath & "\"
End If
GetDirectorySize(Rootpath)
strsize = FormatNumber(fp.FileSize / (1024 * 1024), 2) & ";" & fp.FileCount
TogetDirectorySize = strsize.Split(";")
End Function
#End Region

Nice one