Sign in Join
Wiki copytemporary

Free disk space

It is not obvious how to get the free space, in a directory. Here is working code from the CudaText project.

pascal
uses SysUtils;

function AppDiskGetFreeSpace(const fn: string): Int64;
begin
  {$ifdef linux}
  //this crashes on FreeBSD 12 x64
  exit(SysUtils.DiskFree(SysUtils.AddDisk(ExtractFileDir(fn))));
  {$endif}

  {$ifdef windows}
  exit(SysUtils.DiskFree(SysUtils.GetDriveIDFromLetter(ExtractFileDrive(fn))));
  {$endif}

  //cannot detect
  exit(-1);
end;

The "fn" parameter should be any filename in the needed directory. E.g. if we need to get free space in the directory '/home/user/media', pass parameter fn as '/home/user/media/a'.

Note: code does not support Unixes other than Linux, because real tests show, that code for "$ifdef linux" part is crashing on FreeBSD 12 x64. Under FPC 3.2-fixes. Gitlab issue.


From the Free Pascal wiki, page "Free disk space", as it stood on 2022-01-30 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
AlextpCreated page
Alextp
Alextp
AlextpAlextp moved page Disk free space to Free disk space: use proper English language (minor)
Alextp

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.