473,837 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining the Size of a Directory Without Using FileSystemObjec t

Using the following, you can determine the size of a file:

Dim fi As New IO.FileInfo(<Pa th to file>)
MsgBox(fi.Lengt h)

.... but what about the size of a directory? The IO.DirectoryInf o 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
FileSystemObjec t? Thanks.
Nov 21 '05 #1
2 7903
Hi Phil,

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

Private Function DirectorySize(B yVal sDir As String) As Long
Dim d As New IO.DirectoryInf o(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.Get FolderPath(Envi ronment.Special Folder.Desktop)
' Variable to convert direct size into bytes
Dim intBytes As Int32 = DirectorySize(s trDir)
' 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
Nov 21 '05 #2
I decided to code you a recursive directory size function:

Public Shared Function GetDirectorySiz e(ByVal sPath As String) As Long
Dim lngSize As Long = 0
Dim diDir As New System.io.Direc toryInfo(sPath)
Dim fil As System.io.FileI nfo
For Each fil In diDir.GetFiles( )
lngSize += fil.Length
Next fil
' Recursively call the function
Dim subDirInfo As System.io.Direc toryInfo
For Each subDirInfo In diDir.GetDirect ories()
lngSize += GetDirectorySiz e(subDirInfo.Fu llName)
Next subDirInfo
Return lngSize
End Function

Usage:
--------

Dim strDir As String =
Environment.Get FolderPath(Envi ronment.Special Folder.Desktop)
MessageBox.Show (GetDirectorySi ze(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
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2692
by: Ken | last post by:
Is there a way to check the file size without uploading it? Check it at the source? If a large file (4m) is accidentally selected, it takes some time before the upload is complete. Is there a way to determine the directory from which the file is being uploaded (source directory)?
5
2214
by: bart plessers | last post by:
Hello, Currently I am developping a internet "directory browser" My page 'default.asp' has following entries: CurrentPATH = Request("MyLink") Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set oFolderContents = oFolder.Files
4
11850
by: Bart Plessers \(artabel\) | last post by:
Hello, I have an asp script that lists the files in a directory: CurrentPATH = "c:\temp\" Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set oFolderContents = oFolder.Files For Each oFileItem in oFolder.Files .....
9
9807
by: Ed | last post by:
Is there any known issue with the use of "Scripting.FileSystemObject"? Here's a snippet of my code to obtain the filesize of a file Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.Getfile(strFileSpec) lngFileSize = f.size Set fso = Nothing This used to work perfectly. But with the grown of files (now more than
5
3344
by: Ken Fine | last post by:
I want my application to maintain a directory tree based on months and years, e.g.: 2004 January file file file February file
26
2484
by: Dan Nash | last post by:
Hi guys I have a page that is *supposed* to list the directories on the server. Here's the code... folderspec = server.MapPath("./images/") set fso = CreateObject("Scripting.FileSystemObject") set fold = fso.GetFolder(folderspec) for each subfolder in fold.subFolders Response.Write(subfolder.name & "<br>")
2
5427
by: Luca | last post by:
I have the following problem: I'm developing a system where there are some processes that communicate each other via message queues; the message one process can send to another process is as follows: ****************************************** struct ST_MSG { int iType; char aData; }
2
2256
by: Phil Galey | last post by:
In VB.NET I find the IO object very handy in replacing most of the functionality of the FileSystemObject. One exception, however, is in determining the size of a file. How can you determine the size of a file in VB.NET without adding the FileSystemObject to your project?
1
5468
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one group get automatically re-directed after uploading. However, this member group never gets the benefit of knowing if they've uploaded an incorrect file size or incorrect file extension. Members from the second group do see the "exceeds max file...
0
9837
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10870
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10267
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9392
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6998
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.