Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. calculating the size of all the files present in a folder

calculating the size of all the files present in a folder

Scheduled Pinned Locked Moved Visual Basic
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    harish139
    wrote on last edited by
    #1

    i want to calculate the size of all the files present in a folder how vcan i do this thank u in advance

    C 1 Reply Last reply
    0
    • H harish139

      i want to calculate the size of all the files present in a folder how vcan i do this thank u in advance

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • C Colin Angus Mackay

        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

        T Offline
        T Offline
        Tyquaun Hunter
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • T Tyquaun Hunter

          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

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Tyquaun Hunter wrote:

          Note: I am not sure if you are using vb.net or asp.net

          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

          T 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Tyquaun Hunter wrote:

            Note: I am not sure if you are using vb.net or asp.net

            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

            T Offline
            T Offline
            Tyquaun Hunter
            wrote on last edited by
            #5

            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

            H 1 Reply Last reply
            0
            • T Tyquaun Hunter

              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

              H Offline
              H Offline
              harish139
              wrote on last edited by
              #6

              i am using only Visual Basic.thank u for the solution

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups