Get Available Space and Encapsulated Browse for Folder - January 2001

Periodically there are requests in the news groups for functions that return available disk space. I 'dusted off' some code I wrote some time ago, added support for 'large' drives and while I was at it encapsulated 'Browse for Folder' code that was written by Randy Birch and others. Randy's web site: http://www.mvps.org/vbnet/index.html 

Demo Program

I wrote a VB 6 demo program to show how to use the functions and to call the 'Browse for Folder' class module. The demo:


Get Available Space on Drive D.


Browse for Folder

Use DoGetDiskFreeSpaceEX and DoGetDiskFreeSpace

Function DoGetDiskFreeSpaceEX is called first, if it fails then DoGetDiskFreeSpace is called. DoGetDiskFreeSpaceEX supports large drives, many gigs, whereas DoGetDiskFreeSpace is the older version that only supports drives up to 2 gig. A sample call:

If DoGetDiskFreeSpaceEX(sDrive, dBytesAvailable, dBytesTotal) Then
   'do something with dBytesAvailable
End If

Windows 2000 supports user space allocation. That is, you can define user quotas. If you are using Windows 2000 then these functions return the amount of space available to the user, not the amount of free space on the drive. 

Encapsulated Browse for Folder

I added a 'browse for folder' button to the demo. I planned to use some old code that I had downloaded from somewhere, some of it from Randy Birch. You can go to almost any VB web site and get 'browse for folder' code. But I could not find any that was encapsulated, so I took the code and encapsulated it in class module 'cBrowseFolder.cls'. A sample call:

Dim oBrowseFolder As New cBrowseFolder
Dim sFolder As String

' Trim and add a back slash if needed
sFolder = AddBackslash(Trim$(Me.Text1(0).Text))

With oBrowseFolder   'define object
    .lhWnd = Me.hWnd 'owner
    .sTitle = "Select a Drive" 'title
    .sFolder = sFolder 'initial folder
    .lFlags = BIF_ReturnOnlyFSDirs 'default
    sFolder = .ShowBrowse() 'go get it
    If Not .bCancelled Then
        Me.Text1(0).Text = sFolder
    End If
End With

Set oBrowseFolder = Nothing

Include In Your Application

To get available disk space.

modGetDiskFreeSpace.bas Provides support for DoGetDiskFreeSpaceEX
and DoGetDiskFreeSpace.

To use 'browse for folder'.

cBrowseFolder.cls The class module used to browse for a folder.
mBrowseFolder.bas Contains functions and constants used by 
the class module.

Download VB 6 Source Code

Click here to download the VB 6 source code [11k]

Previous Tip   Tip Index    Next Tip

Home

Updated 2001/02/25