473,670 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get the total size of a folder?

g'day group,

I need some code that will give me the total size of a folder. In the
MSDN I've found some code, but I cannot figure out how to use this code
at all ...

I am quite new to .NET, I have been using VB6 always. Can anyone point
me in the right direction?

How will the following code give me the total size of al files in
folder "c:\test" ? Where do I put this code? And how do I get the total
size out of this class and function?
thanks,
Ed
--------------------

Imports System
Imports System.IO
Imports Microsoft.Visua lBasic
' the above is already at the right spot in the code

Public Class ShowDirSize

Public Shared Function DirSize(ByVal d As DirectoryInfo) As
Long
Dim Size As Long = 0
' Add file sizes.
Dim fis As FileInfo() = d.GetFiles()
Dim fi As FileInfo
For Each fi In fis

Size += fi.Length
Next fi
' Add subdirectory sizes.
Dim subs() As Boolean

'If subs(counter) = True Then

Dim dis As DirectoryInfo() = d.GetDirectorie s()
Dim di As DirectoryInfo
For Each di In dis
Size += DirSize(di)
Next di
Return Size

'End If

End Function 'DirSize

Public Overloads Shared Sub Main(ByVal args() As String)
If args.Length <> 1 Then
Console.WriteLi ne("You must provide a directory
argument at the command line.")
Else
Dim d As New DirectoryInfo(a rgs(0))
Console.WriteLi ne("The size of {0} and its
subdirectories is {1} bytes.", d, DirSize(d))
End If
End Sub 'Main
End Class 'ShowDirSize

Nov 21 '05 #1
12 4111
never mind, found some other code, much shorter, doing the job:

Private Function DirSize(ByVal path As String) As Long
Dim sz As Long = 0
Dim d As DirectoryInfo = New DirectoryInfo(p ath)
' get file length
Dim f As FileInfo
For Each f In d.GetFiles()
sz += f.Length
Next
' recurse into directories
Dim dx As DirectoryInfo
For Each dx In d.GetDirectorie s()
sz += DirSize(dx.Full Name)
Next
Return sz
End Function

Nov 21 '05 #2
Grouped,

You can better use the Integer as indexer in VBNet, that it for the 32Bit
computer the most optimize one.

I hope this helps,

Cor
Nov 21 '05 #3
"grouped200 0" <gr*********@gm ail.com> schrieb:
I need some code that will give me the total size of a folder.


That's not as easy:

Computing the size of a directory is more than just adding file sizes
<URL:http://blogs.msdn.com/oldnewthing/archive/2004/12/28/336219.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
Lon
Hi,

But the Intereger data type only has a limited range.. and when calculating
direcotry sizes the long data type is better as its range is larger
(Specialy for larger directories..)

Greetz
Lon
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:OL******** ******@tk2msftn gp13.phx.gbl...
Grouped,

You can better use the Integer as indexer in VBNet, that it for the 32Bit
computer the most optimize one.

I hope this helps,

Cor

Nov 21 '05 #5
Lon,
But the Intereger data type only has a limited range.. and when
calculating direcotry sizes the long data type is better as its range is
larger
(Specialy for larger directories..)

Interesting, can you tell us how many files that there can be in a directory
and how much you can index with an integer and how much with a long?

Cor
Nov 21 '05 #6
"Cor Ligthert" <no************ @planet.nl> schrieb:
But the Intereger data type only has a limited range.. and when
calculating direcotry sizes the long data type is better as its range is
larger
(Specialy for larger directories..)


Interesting, can you tell us how many files that there can be in a
directory and how much you can index with an integer and how much with a
long?


When summing up file sizes, I would use a 'Long' too. Maybe 'ULong' would
be preferrable because it can deal with larger numbers.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7
Given the fact that FileInfo.Length returns a long, I would suggest he sum
it up in a long.

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:eQ******** ******@TK2MSFTN GP09.phx.gbl...
Lon,
But the Intereger data type only has a limited range.. and when
calculating direcotry sizes the long data type is better as its range is
larger
(Specialy for larger directories..)

Interesting, can you tell us how many files that there can be in a
directory and how much you can index with an integer and how much with a
long?

Cor

Nov 21 '05 #8
Herfried,

I was talking about the indexer to count the files.

You probably don't believe it, I think I know the value that an integer
posivite and negatieve can contain.

With the integer is possible more than 4.000.000.000 which is a lot in of
files in one folder in my opinion.

I was not talking about file size.

However if you want to say that for this kind of cound indexers better a
long can be used than am I really curious for your explanation.

Cor
Nov 21 '05 #9
Anon,

I was talking about the indexer to count the files, not the file length.

Cor
Nov 21 '05 #10

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

Similar topics

7
2822
by: rick | last post by:
Can anyone help, I am try to create a simple form using a table, where a user can fill out quanty and price and have a total automatically calculated and inserted in another field. I stuck trying to figure out how expand this script to recalculate when rows are added or removed. My code so far. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4
6366
by: Rich_C | last post by:
I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost (cost). Users can dynamically add rows to the table so I don't know how many rows might need to be calculated. I need to calculate the total (qty * cost) and put that number in a table cell (or read only input box). I also need to sum the...
4
6488
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method return the total number of <foldernodes (9) regardless of calling from the <parent1> and <parent2node. XML: ----------------------------------
2
5064
by: sammiesue | last post by:
Hi, I have form with 2 autosummed textboxes ("total" and "casinototal"). I would like to have a grand total textbox ("grandtotal") get its value from summing "total" and "casinototal", but it doesn't seem to be working. Here is my code. Thanks for your help. -sammie <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Nursing Conference 2007 Registration Form</title>
10
2445
meenakshia
by: meenakshia | last post by:
hi forum:) i have a form in which i have four input fields for pieces to be entered and 4 fields for amount what i want is that the first pieces-t1 should be visible and rest three should not show up on the form unless asked for. can anyone suggest me a way to do this i have come across a lot of help areas where we can add input fields but in my case i have only predefined 3 fields the code is below <td>Pieces-t1</td> <td><input...
10
6751
by: programmerboy | last post by:
As the subject says how can I check total # of files and folders in a particular folder and determine the total size of that folder. I will be using VB.NET. A small code snippet will be great. Thanks
8
1849
by: W. eWatson | last post by:
I have an ordinary text file with a CR at the end of a line, and two numbers in each line. Is there some way to determine the number of lines (records) in the file before I begin reading it? -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
5
2845
by: bharathreddy | last post by:
How to find the size of a folder using C# code? step1: Here take the folder which u want to find the size, then pass that folder to the recursive function name FolderSize!. DirectoryInfo dirInfo = new DirectoryInfo(strPath) ; decimal dSize = 0; //If the folder exists then only its size will be calculated if not its size will be //zero! if (dirInfo.Exists)
2
2600
by: sumanta123 | last post by:
Dear Sir, In my develpment i am getting stuck for a senario.Kindly please help me for it. Everytime we give the request we get the response of 8 records and its corresponding value. Then next button will display more and more records if it is availbale in data base. I am attachcing the screen shoot of my problem CMP is a column in the this record.
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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
8813
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
8591
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,...
1
6212
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
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();...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.