Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I determine the available disk space on a drive?

System Information

How do I determine the available disk space on a drive?

by  Stretchwickster  Posted    (Edited  )
This method returns the free space and capacity of the drive specified by DriveLetter.
Code:
function GetAvailableSpace(DriveLetter: Char): String;
var
  DiskCapacity, FreeSpace: Extended;
begin
  DriveLetter := UpCase(DriveLetter);
  DiskCapacity := DiskSize(Ord(DriveLetter) - 64) / 1048576;
  FreeSpace := DiskFree(Ord(DriveLetter) - 64) / 1048576;
  if DiskCapacity <= 0 then
    Result := 'Disk information not available'
  else if FreeSpace > 1000 then
  begin
    DiskCapacity := DiskCapacity / 1024;
    FreeSpace := FreeSpace / 1024;
    Result := FormatFloat('#,##0.00', FreeSpace) + 'GB of ' + FormatFloat('#,##0.00', DiskCapacity) + 'GB';
  end
  else
    Result := FormatFloat('#,##0', FreeSpace) + 'MB of ' + FormatFloat('#,##0', DiskCapacity) + 'MB';
end;
An example of its use is in the code below. The following code runs through drives B to Z, checks if the drive is a hard drive (i.e. a fixed disk) before calling the GetAvailableSpace function, and adds the returned string to ValueListEditor1.
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  driveLetter: Char;
begin
  for driveLetter := 'B' to 'Z' do
  begin
    if GetDriveType(PChar(driveLetter + ':\')) = DRIVE_FIXED then
      ValueListEditor1.InsertRow('Available Space (' + driveLetter + ':)',
       GetAvailableSpace(driveLetter), True);
  end;
end;
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top