473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.VisualBasic
' 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.GetDirectories()
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.WriteLine("You must provide a directory
argument at the command line.")
Else
Dim d As New DirectoryInfo(args(0))
Console.WriteLine("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 4085
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(path)
' 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.GetDirectories()
sz += DirSize(dx.FullName)
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
"grouped2000" <gr*********@gmail.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**************@tk2msftngp13.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**************@TK2MSFTNGP09.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
Where in his code does he count the files?

"Cor Ligthert" <no************@planet.nl> wrote in message
news:u1**************@tk2msftngp13.phx.gbl...
Anon,

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

Cor

Nov 21 '05 #11
Anon,

You are right, I misreaded it, however my advice was to use for indexers an
integer.

And your message was connected to mine, not to somebody else.

Cor
Nov 21 '05 #12
"Cor Ligthert" <no************@planet.nl> schrieb:
I was talking about the indexer to count the files.


Nobody except you was talking about counting files.

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

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

Similar topics

7
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...
4
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...
4
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...
2
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...
10
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...
10
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
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? --...
5
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...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.