INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

System Information

How do I determine the available disk space on a drive? by Stretchwickster
Posted: 27 Feb 03 (Edited 5 Oct 05)

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;

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close