PC Review


Reply
Thread Tools Rate Thread

Determining the Size of a Directory Without Using FileSystemObject

 
 
Phil Galey
Guest
Posts: n/a
 
      17th Mar 2005
Using the following, you can determine the size of a file:

Dim fi As New IO.FileInfo(<Path to file>)
MsgBox(fi.Length)

.... but what about the size of a directory? The IO.DirectoryInfo object
doesn't have a Length property.
In is there a way in VB.NET to determine the size of a directory without
having to resort to importing the Scripting DLL and using the
FileSystemObject? Thanks.


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q3JvdWNoaWUxOTk4?=
Guest
Posts: n/a
 
      17th Mar 2005
Hi Phil,

Here is one way you can do it, but it's not a recursive directory size:

Private Function DirectorySize(ByVal sDir As String) As Long
Dim d As New IO.DirectoryInfo(sDir)
Dim fils As IO.FileInfo() = d.GetFiles()
Dim f As IO.FileInfo
Dim i As Integer
For Each f In fils
i += f.Length
Next
Return i
End Function

Usage:
--------

' Directory to check for size
Dim strDir As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
' Variable to convert direct size into bytes
Dim intBytes As Int32 = DirectorySize(strDir)
' Variable to convert direct size into kilobytes
Dim intKiloBytes As Int32 = (DirectorySize(strDir) \ 1024)
' Variable to convert direct size into megabytes
Dim intMegaBytes As Int32 = (DirectorySize(strDir) \ 1048576)
' Variable to convert direct size into gigabytes
Dim intGigaBytes As Int32 = (DirectorySize(strDir \ 1073741824))
' Show results in bytes
MessageBox.Show(String.Format("The directory size is {0:d2} bytes",
intBytes))
' Show results in kilobytes
MessageBox.Show(String.Format("The directory size is {0} KB",
intKiloBytes))
' Show results in megabytes
MessageBox.Show(String.Format("The directory size is {0} MB",
intMegaBytes))
' Show results in gigabytes
MessageBox.Show(String.Format("The directory size is {0} Gb",
intGigaBytes))

I hope this helps
 
Reply With Quote
 
=?Utf-8?B?Q3JvdWNoaWUxOTk4?=
Guest
Posts: n/a
 
      18th Mar 2005
I decided to code you a recursive directory size function:

Public Shared Function GetDirectorySize(ByVal sPath As String) As Long
Dim lngSize As Long = 0
Dim diDir As New System.io.DirectoryInfo(sPath)
Dim fil As System.io.FileInfo
For Each fil In diDir.GetFiles()
lngSize += fil.Length
Next fil
' Recursively call the function
Dim subDirInfo As System.io.DirectoryInfo
For Each subDirInfo In diDir.GetDirectories()
lngSize += GetDirectorySize(subDirInfo.FullName)
Next subDirInfo
Return lngSize
End Function

Usage:
--------

Dim strDir As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
MessageBox.Show(GetDirectorySize(strDir))

The above example will get the directory size of the Desktop including all
sub directories.

If you use the conversions from my other post, you can soon put the info
into bytes, kilobytes, megabytes & gigabytes

I hope this helps
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining if a directory is writeable Marco Shaw Microsoft Dot NET 1 23rd Nov 2006 06:35 PM
Counting Files in a directory with FileSystemObject Andrew Microsoft Access Form Coding 2 3rd Jul 2004 02:38 PM
Determining A Valid Directory Ross Microsoft Access VBA Modules 2 3rd Feb 2004 04:47 AM
Determining if a path is a directory or a file Jeremy Chapman Microsoft Dot NET Framework 1 7th Nov 2003 01:05 AM
Re: Determining if Active Directory is being used on a server. Mark Ramey Microsoft Windows 2000 Active Directory 0 11th Jul 2003 11:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:57 AM.