Page 1 of 1

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!

Clear all

[Solved] Lazarus - MacOS - Determine available disk space

1 Posts
1 Users
Famed Member Admin
Joined: 11 years ago
Posts: 2678
Topic starter  

For one of my projects (ApplePi-Baker) I needed the ability to see if there is enough disk space available to store a temporary file.
Unfortunately, some of the system API's give very interesting numbers that were of no use to me.
The function "df" does give good info, but I wanted to avoid using command-line tool.

So this is what I was looking for;

Given the path of a file, for example "/Volumes/LIBREELEC/overlays" (USB drive), or "/users/Username" (Mac system disk), I wanted to know how much space was available on the disk this fpath was stored on.

Lazarus Pascal keeps amazing me with its handy little tools, and this is how it's done (MacOS and probably Linux as well).
First we need to "add" the "disk" using "AddDisk" - it appears we can just enter the path where we'd like to store our data later.
This returns a byte to identify the disk, which is then passed to the function "DiskFree".

uses ... SysUtils ...; // default included in a project
  diskID := AddDisk('/Volumes/LIBREELEC/overlays');
  ShowMessage('Available disk space = '+ IntToStr( DiskFree(diskID) ) + ' bytes');

And that's all there is to it ...