By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Size of a folder on disk.

P: n/a
What is the best way to find the size of a folder (actually it's contents)
on a disk?
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* "Ty Moffett" <tm******@solarc.com> scripsit:
What is the best way to find the size of a folder (actually it's contents)
on a disk?


Sample in C#:

<http://www.google.com/groups?selm=JrXeg5sQCHA.1788%40cpmsftngxa10>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #2

P: n/a
"Ty Moffett" <tm******@solarc.com> wrote...
What is the best way to find the size of a folder (actually it's contents)
on a disk?


I converted the C# code...

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 20 '05 #3

P: n/a
* "Tom Leylan" <ge*@iamtiredofspam.com> scripsit:
What is the best way to find the size of a folder (actually it's contents)
on a disk?


I converted the C# code...


Thank you for doing that! The next time this question is posted we have
a solution written in VB.NET.

:-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #4

P: n/a
Heh, just a I got it converted I noticed your post. At least I know I'm
learning something. Thanks fellas. =)

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bo*************@ID-208219.news.uni-berlin.de...
* "Tom Leylan" <ge*@iamtiredofspam.com> scripsit:
What is the best way to find the size of a folder (actually it's contents) on a disk?


I converted the C# code...


Thank you for doing that! The next time this question is posted we have
a solution written in VB.NET.

:-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>

Nov 20 '05 #5

P: n/a

I am shooting for an app that will iterate through a directory tree and
return the size of each folder in a tree along with the path. i.e. 972463
c:\temp. Using the code provided earlier I can easily get the total size at
the end to display in my text window (set to multiline = true). The trouble
comes with this line "txtOutput.Text = txtOutput.Text + di.FullName & " " &
size & vbCrLf". As I step through the program in the debugger I can see
txtOutput.Text being modified with exactly the information I am wanting but
when the program finishes all that is in the textbox is the total size, not
the path information that I see during debugging. What am I missing?

Imports System.IO

Public Class Form1

Inherits System.Windows.Forms.Form

Public Function DirSize(ByVal path As String) As String

Dim size As Long = 0

Dim di As New System.IO.DirectoryInfo(path)

'Get file length

For Each f As System.IO.FileInfo In di.GetFiles()

size += f.Length

Next

txtOutput.Text = txtOutput.Text + di.FullName & " " & size & vbCrLf

'Recurse into directories

For Each dx As System.IO.DirectoryInfo In di.GetDirectories()

size += DirSize(dx.FullName)

Next

Return size

End Function

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnBrowse.Click

FolderBrowserDialog1.ShowDialog()

txtPath.Text = FolderBrowserDialog1.SelectedPath

End Sub

Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click

txtOutput.Text = txtOutput.Text + DirSize(txtPath.Text) + vbCrLf

End Sub

End Class

"Ty Moffett" <tm******@solarc.com> wrote in message
news:Ob*************@TK2MSFTNGP10.phx.gbl...
Heh, just a I got it converted I noticed your post. At least I know I'm
learning something. Thanks fellas. =)

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bo*************@ID-208219.news.uni-berlin.de...
* "Tom Leylan" <ge*@iamtiredofspam.com> scripsit:
> What is the best way to find the size of a folder (actually it's contents)> on a disk?

I converted the C# code...


Thank you for doing that! The next time this question is posted we have
a solution written in VB.NET.

:-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>


Nov 20 '05 #6

P: n/a
"Ty Moffett" <tm******@solarc.com> wrote..
comes with this line "txtOutput.Text = txtOutput.Text + di.FullName & " " & What am I missing?


Could be the "+" symbol.
Nov 20 '05 #7

P: n/a
"Ty Moffett" <tm******@solarc.com> schrieb
Public Function DirSize(ByVal path As String) As String


The return value should be "As Long"
--
Armin

Nov 20 '05 #8

P: n/a
* "Ty Moffett" <tm******@solarc.com> scripsit:
comes with this line "txtOutput.Text = txtOutput.Text + di.FullName & " " &
size & vbCrLf". As I step through the program in the debugger I can see
txtOutput.Text being modified with exactly the information I am wanting but
when the program finishes all that is in the textbox is the total size, not
the path information that I see during debugging. What am I missing? Public Function DirSize(ByVal path As String) As String
'DirSize' should return a 'Long', not a 'String'.
Dim size As Long = 0

Dim di As New System.IO.DirectoryInfo(path)

'Get file length

For Each f As System.IO.FileInfo In di.GetFiles()

size += f.Length

Next

txtOutput.Text = txtOutput.Text + di.FullName & " " & size & vbCrLf


Use the '&' operator to concatenate strings.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.