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" _
( ByVal lpFileName As String, _
ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr

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
''' added by jk on 24th Dec 2007
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


Comments

Author: Rama Krishna K01 Aug 2008 Member Level: Gold   Points : 0

Nice one

Author: Al Mendieta25 Aug 2009 Member Level: Bronze   Points : 2

Hi. I've tried this code in vb.net 2005 but simply don't works. Anyway did some changes (see below), and works, but I have no idea how can format

nFileSizeHigh and nFileSizeLow

to get FileSize and GetDirectorySize?

Can you give me a hand, pls?

Thank you in advance.

LAM





Imports System.Runtime.InteropServices
Imports System.IO
'Imports Microsoft.Win32.SafeHandles


Module Module1

#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 WIN32_FIND_DATA
Public dwFileAttributes As IO.FileAttributes
Public ftCreationTime As FILETIME
Public ftLastAccessTime As FILETIME
Public ftLastWriteTime As FILETIME
Public nFileSizeHigh As Integer
Public nFileSizeLow As Integer
Public dwReserved0 As Integer
Public dwReserved1 As Integer
_
Public cFileName As String
_
Public cAlternate As String
End Structure

Structure FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure


_
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" _
(ByVal lpFileName As String, _
ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr

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

Private 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 FSize As Double

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
''' added by jk on 24th Dec 2007
Public Function GetDirectorySize(ByVal sRoot As String)
Dim wfd As New 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
Debug.Print(wfd.cFileName & " | " & wfd.nFileSizeHigh.ToString & " | " & fp.FileSize.ToString)
If (wfd.cFileName.ToString() <> "." And wfd.cFileName.ToString() <> "..") Then
If (wfd.dwFileAttributes And vbDirectory) Then
GetDirectorySize(sRoot & wfd.cFileName.Trim() & "\")
'FSize = (Fp.ze.dwHighSize * (MAXDWORD + 1) + FSize.dwLowSize)
Debug.Print("------")
Debug.Print(FSize.ToString)
Debug.Print("------")

'FSize = FSize + wfd.nFileSizeLow
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
GetDirectorySize = FSize
FindClose(hFile)
'GetDirectorySize = fp.FileSize
End Function

'''
''' 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
Debug.Print(GetDirectorySize(Rootpath))

strsize = FormatNumber(fp.FileSize / (1024 * 1024), 2) & ";" & fp.FileCount
TogetDirectorySize = strsize.Split(";")
End Function

#End Region


End Module

Author: Al Mendieta26 Aug 2009 Member Level: Bronze   Points : 2

Hi Again. Good news! Case solved. Create a class named GetDirSize and paste next code:

I hope you can find quickly this code. Take 2 days research and code it, taking like base Jeya Kumar's work. Works smooth in VB.NET 2005... Enjoy it!

Luis Alberto Mendieta, almend



Imports System.Runtime.InteropServices
Imports System.IO
Public Class GetDirSize
Declare Auto Function FindFirstFile Lib "kernel32.dll" (ByVal lpFileName As String, _
ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr

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

Private WFD As WIN32_FIND_DATA = New WIN32_FIND_DATA

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

'Private FSize As Long
'Private fCounter As Integer

_
Structure WIN32_FIND_DATA
Public dwFileAttributes As UInteger
Public ftCreationTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public ftLastAccessTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public ftLastWriteTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public nFileSizeHigh As UInteger
Public nFileSizeLow As UInteger
Public dwReserved0 As UInteger
Public dwReserved1 As UInteger
Public cFileName As String
Public cAlternateFileName As String
End Structure


Structure FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure

Private FSize As Long

' added by jk on 24th Dec 2007
Public Function GetDirectorySize(ByVal sRoot As String) As Long
Dim wfd As New WIN32_FIND_DATA
Dim hFile As Long


hFile = FindFirstFile(sRoot & "*", wfd)


If hFile <> INVALID_HANDLE_VALUE Then
Do
Try
Application.DoEvents()
'Debug.Print(wfd.cFileName.ToString & " " & ((wfd.nFileSizeHigh * MAXDWORD + 1) + wfd.nFileSizeLow))

FSize += ((wfd.nFileSizeHigh * MAXDWORD + 1) + wfd.nFileSizeLow)

If (wfd.cFileName.ToString() <> "." And wfd.cFileName.ToString() <> "..") Then
If (wfd.dwFileAttributes And vbDirectory) Then
GetDirectorySize(sRoot & wfd.cFileName.Trim() & "\")
'Else
' fCounter += 1
End If

End If

Catch ex As Exception
Continue Do
End Try
Loop While FindNextFile(hFile, wfd)
End If
Return FSize '& "|" & fCounter
FindClose(hFile)
Exit Function
End Function
Private fCounter As Integer
Public Function GetDirectoryFileCount(ByVal sRoot As String) As String
Dim wfd As New WIN32_FIND_DATA
Dim hFile As Long


hFile = FindFirstFile(sRoot & "*", wfd)


If hFile <> INVALID_HANDLE_VALUE Then
Do
Try
Application.DoEvents()


If (wfd.cFileName.ToString() <> "." And wfd.cFileName.ToString() <> "..") Then
fCounter += 1

End If

Catch ex As Exception
Continue Do
End Try
Loop While FindNextFile(hFile, wfd)
End If
Return fCounter
FindClose(hFile)
End Function


Public Function SetPath(ByVal instring As String) As String
'appends a back slash to a path if needed

If Right$(instring, 1) <> "\" Then
instring = instring & "\"
End If

SetPath = instring

End Function

Private Function StripNulls(ByVal OriginalStr As String) As String
'strip nulls from a string

If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left$(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If

StripNulls = OriginalStr

End Function

End Class



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: