calculating the size of all the files present in a folder
-
i want to calculate the size of all the files present in a folder how vcan i do this thank u in advance
Get a list of all the files, loop over each file and get its size. As you loop you accumulate the size in a variable. At the end of the loop your variable will contain the total size of all files in the directory you were looping over.
Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
Get a list of all the files, loop over each file and get its size. As you loop you accumulate the size in a variable. At the end of the loop your variable will contain the total size of all files in the directory you were looping over.
Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
To simplify the steps, I have created two functions you can use. 1. The first function will get all files in specified directory. You will need to add them all up to get a total folder size. 2. The second function will convert from bytes to kilobytes. * Note: I am not sure if you are using vb.net or asp.net. I used Vb.Net so you will see a message box in my first function Enjoy! 'Get File sizes in directory Public Shared Sub GetFilesSizesInDirectory(ByVal Dir As String) 'Declare a variable to recieve file name Dim FileName As String 'Iterate through specified directory For Each FileName In Directory.GetFiles(Dir) 'Get file info and out file size Dim Info As FileInfo = New FileInfo(FileName) MsgBox(FileName & " " & ToKilobytes(Info.Length) & " KB") Next End Sub 'Convert from bytes to kilobytes Public Shared Function ToKilobytes(ByVal Key As Long) As Long 'Variable to recieve remainder Dim Remainder As Long 'Result of calculation Dim Result As Integer = Math.DivRem(Key, 1024, Remainder) 'If there is a remainder always round-up If Remainder <> 0 Then Result += 1 End If Return Result End Function Tyquaun -- modified at 22:53 Thursday 27th July, 2006
-
To simplify the steps, I have created two functions you can use. 1. The first function will get all files in specified directory. You will need to add them all up to get a total folder size. 2. The second function will convert from bytes to kilobytes. * Note: I am not sure if you are using vb.net or asp.net. I used Vb.Net so you will see a message box in my first function Enjoy! 'Get File sizes in directory Public Shared Sub GetFilesSizesInDirectory(ByVal Dir As String) 'Declare a variable to recieve file name Dim FileName As String 'Iterate through specified directory For Each FileName In Directory.GetFiles(Dir) 'Get file info and out file size Dim Info As FileInfo = New FileInfo(FileName) MsgBox(FileName & " " & ToKilobytes(Info.Length) & " KB") Next End Sub 'Convert from bytes to kilobytes Public Shared Function ToKilobytes(ByVal Key As Long) As Long 'Variable to recieve remainder Dim Remainder As Long 'Result of calculation Dim Result As Integer = Math.DivRem(Key, 1024, Remainder) 'If there is a remainder always round-up If Remainder <> 0 Then Result += 1 End If Return Result End Function Tyquaun -- modified at 22:53 Thursday 27th July, 2006
Tyquaun Hunter wrote:
What is he's using VB.NET and ASP.NET? VB.NET is a language. ASP.NET is a framework for writing web applications. The two are not mutually exclusive. I think what you meant to say is: I'm not sure if you're using a Windows Forms application or a Web Forms application.... Actually, that's not really correct either, because what if it is a console application, or a windows service.... Never mind. It's Three in the Morning and it is too hot to sleep.
Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
-
Tyquaun Hunter wrote:
What is he's using VB.NET and ASP.NET? VB.NET is a language. ASP.NET is a framework for writing web applications. The two are not mutually exclusive. I think what you meant to say is: I'm not sure if you're using a Windows Forms application or a Web Forms application.... Actually, that's not really correct either, because what if it is a console application, or a windows service.... Never mind. It's Three in the Morning and it is too hot to sleep.
Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog
Actually, you are right. However, what I have noticed is when people say VB.net they mean winforms and asp.net, well it is assumed I am talking about ASP.net/VB.net because I am in a VB.net forum. -- modified at 22:44 Thursday 27th July, 2006
-
Actually, you are right. However, what I have noticed is when people say VB.net they mean winforms and asp.net, well it is assumed I am talking about ASP.net/VB.net because I am in a VB.net forum. -- modified at 22:44 Thursday 27th July, 2006