Click here to Skip to main content
13,048,584 members (92,781 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.2K views
1 bookmarked
Posted 19 Jul 2010

Print File Size

, 19 Jul 2010
Rate this:
Please Sign up or sign in to vote.
You can keep your output consistent with how Windows represents file sizes by using the inbuilt function;StrFormatByteSizeThe following code will dump the files from the root of C:\, Call getFilesAndSizes from a button click or wherever you want. ...
You can keep your output consistent with how Windows represents file sizes by using the inbuilt function;StrFormatByteSize

The following code will dump the files from the root of C:\, Call getFilesAndSizes from a button click or wherever you want.

<pre lang="vb"">
<System.Runtime.InteropServices.DllImport("shlwapi", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function StrFormatByteSize( _
ByVal fileSize As Long, _
ByVal buffer As System.Text.StringBuilder, _
ByVal bufferSize As Integer
) As Long
End Function

Private Sub GetFilesAndSizes()
dirFiles("C:\")
End Sub

Private Sub dirFiles(ByVal path As String)

Dim fi As System.IO.FileInfo
Dim sb As New System.Text.StringBuilder
Dim files() As String
Try
files = System.IO.Directory.GetFiles(path)

For Each file In files
fi = New System.IO.FileInfo(file)
StrFormatByteSize(fi.Length, sb, 128)

Debug.WriteLine(file & " : " & sb.ToString)
Next

Catch ex As Exception
Debug.WriteLine("Error with: " & path)
End Try

End Sub


The output generated on my laptop was;
C:\autoexec.bat : 24 bytes
C:\bootmgr : 374 KB
C:\BOOTSECT.BAK : 8.00 KB
C:\config.sys : 10 bytes
C:\hiberfil.sys : 2.24 GB
C:\pagefile.sys : 2.99 GB

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DaveAuld
Engineer
Scotland Scotland
I have been working in the Oil & Gas Industry for over 25 years now.

Core Discipline is Instrumentation and Control Systems.

Completed Bsc Honours Degree (B29 in Computing) with the Open University in 2012.

Currently, Offshore Installation Manager in the Al Shaheen oil field, which is located off the coast of Qatar.

Prior to this, 25 years of North Sea Oil & Gas experience.

You may also be interested in...

Comments and Discussions

 
GeneralThat's really cool function! Pin
chkmos23-Jul-10 6:33
memberchkmos23-Jul-10 6:33 
GeneralVery Cool. I hadn't seen that before. I love how it dumps it... Pin
SSDiver211221-Jul-10 13:35
memberSSDiver211221-Jul-10 13:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 19 Jul 2010
Article Copyright 2010 by DaveAuld
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid