Tip 40: Calculating the Number of Bytes Used by Files Stored in a Directory

Created: April 1, 1995

Abstract

This article explains how you can use the Visual Basic® Dir$, FileName, and FileLen functions to calculate the space used by files in a directory.

When DiskSpaceFree Is Not Enough

The DiskSpaceFree function found in SETUPKIT.DLL can tell you the amount of free space available on the specified disk drive. However, if you need to determine how much space is occupied by the files stored in a single directory, you will not be able to use this function.

How, then, can you find out how much space is used by the files? One solution is to open each file in the directory and move the files pointer to the end of the file. Then you can find out how many bytes are stored in the file. This method, however, is far too slow because each file must be individually opened and closed.

A better solution is to use the Dir$, FileName, and FileLen functions in Visual Basic® to scan the directory and keep a running total of the number of bytes in each file:

Example Program

The program below shows how you can use a Do-While loop to calculate how many bytes are occupied by all the files stored in a directory. The Directory variable is set to the path of the directory you want to work with. After the program has determined the length of all files stored in the directory, it displays the result in the Text Box.

  1. Create a new project in Visual Basic. Form1 is created by default.

  2. Add a Text Box control to Form1. Text1 is created by default.

  3. Add the following code to the Form_Load event for Form1:
    Sub Form_Load()
         Dim FileName As String
         Dim FileSize As Currency
         Dim Directory As String
    
         Directory = "c:\windows\system\"
         FileName = Dir$(Directory & "*.*")
         FileSize = 0
    
         Do While FileName <> ""
                FileSize = FileSize + FileLen(Directory & FileName)
                FileName = Dir$
         Loop
    
         Text1.Text = "Total bytes used = " + Str$(FileSize)
    End Sub