tags:

views:

901

answers:

1

I am trying to get all the information about my hard drives displayed in a memo or some other control so I can see free space and total space on the drives. I know there is a shell call I can use, but cannot figure out the usage.

Can anyone give me an example or explain what I am doing wrong?

+87  A: 

I assume you mean Hard Drives. The simplest way is actually to use GetDiskFreeSpaceEx from the sysutils.pas file.

There are 2 parts to this example. the 1st is the important part using GetDiskFreeSpaceEX.

function DriveSpace(DriveLetter : String; var FreeSpace, UsedSpace, TotalSpace : int64) : Boolean;
begin
  Result := SysUtils.GetDiskFreeSpaceEx(Pchar(DriveLetter), UsedSpace, TotalSpace, @FreeSpace);

  if UsedSpace > 0 then
    UsedSpace := TotalSpace - FreeSpace;

  if not Result then
  begin
    UsedSpace   := 0;
    TotalSpace  := 0;
    FreeSpace   := 0;
  end;
end;

If you are going to request drives that you already know the drive letter for such as C: then that is all you need.

Usage would be something like:

var
  FS,
  US,
  TS : Int64
begin
  DriveSpace('C:', FS, US, TS);
  //Do something with the 3 variables.
end;

Having said that if you want to find the drives as well you could use something like this:

procedure ListDrivesOfType(DriveType : Integer; var Drives : TStringList);
var
  DriveMap,
  dMask : DWORD;
  dRoot : String;
  I     : Integer;
begin
  dRoot     := 'A:\'; //' // work around highlighting
  DriveMap  := GetLogicalDrives;
  dMask     := 1;

  for I := 0 to 32 do
  begin
    if (dMask and DriveMap) <> 0 then
      if GetDriveType(PChar(dRoot)) = DriveType then
      begin
        Drives.Add(dRoot[1] + ':');
      end;

    dMask := dMask shl 1;
    Inc(dRoot[1]);
  end;
end;

Note the DriveType integer, should be one of the following:

DRIVE_UNKNOWN     = 0;
DRIVE_NO_ROOT_DIR = 1;
DRIVE_REMOVABLE   = 2;
DRIVE_FIXED       = 3;
DRIVE_REMOTE      = 4;
DRIVE_CDROM       = 5;
DRIVE_RAMDISK     = 6;

(I have taken these straight out of windows.pas)


Now finally to answer your question (and this is very rough) the following would add information into a memo (called memo1) for all FIXED HARD DRIVES:

Procedure TAform.SomeNameICantThinkOfNow;
const
  BytesPerMB = 1048576;
var
  MyDrives   : TStringlist;
  I : Integer;
  FreeSpace,
  UsedSpace,
  TotalSpace : int64;
begin
  MyDrives := TStringlist.Create;
  ListDrivesOfType(DRIVE_FIXED, MyDrives);

  Memo1.Lines.Clear;

  for I := 0 to MyDrives.Count - 1 do
  begin
    FreeSpace  := 0;
    UsedSpace  := 0;
    TotalSpace := 0;

    if DriveSpace(MyDrives.Strings[I], FreeSpace, UsedSpace, TotalSpace) then
    begin
      FreeSpace  := FreeSpace  div BytesPerMB;
      UsedSpace  := UsedSpace  div BytesPerMB;
      TotalSpace := TotalSpace div BytesPerMB;

      Memo1.Lines.Add('Drive: ' + MyDrives.Strings[I] + ' = Free Space :' + IntToStr(FreeSpace) +
                      ' Used Space: ' + IntToStr(UsedSpace) + ' Total Space: ' + IntToStr(TotalSpace));
    end;
  end;
end;

I did say it would be nasty! I have just run this up in the IDE and it works, I have done as MB but really you should convert to Double and choose your formatting if doing as MB to be more precise as the example I have create above will of course just round up.

Hope this is of some small assistance.

Regards

RE

Reallyethical
Do we really need that 1048576 in there?? That should be a constant!
Loren Pechtel
Oh, I think we can forgive the constant. :) @Reallyethical, I hope you enjoy the Reversal and Good Answer badges this answer got you.
John Rudy
Thanks Loren, no I should have used the Const that's the whole point of declaring it, I was rushing to code up an answer for him at the time, not that I would do again. Thanks for the feedback I have amended it.
Reallyethical