Free disk space: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Free disk space
| Old | New | Line | |
|---|---|---|---|
| 1 | + | It is not obvious how to get the free space, in a directory. Here is working code from the [[CudaText]] project. | |
| 2 | + | ||
| 3 | + | ``` Pascal | |
| 4 | + | uses SysUtils; | |
| 5 | + | ||
| 6 | + | function AppDiskGetFreeSpace(const fn: string): Int64; | |
| 7 | + | begin | |
| 8 | + | {$ifdef linux} | |
| 9 | + | //this crashes on FreeBSD 12 x64 | |
| 10 | + | exit(SysUtils.DiskFree(SysUtils.AddDisk(ExtractFileDir(fn)))); | |
| 11 | + | {$endif} | |
| 12 | + | ||
| 13 | + | {$ifdef windows} | |
| 14 | + | exit(SysUtils.DiskFree(SysUtils.GetDriveIDFromLetter(ExtractFileDrive(fn)))); | |
| 15 | + | {$endif} | |
| 16 | + | ||
| 17 | + | //cannot detect | |
| 18 | + | exit(-1); | |
| 19 | + | end; | |
| 20 | + | ``` | |
| 21 | + | ||
| 22 | + | 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'. | |
| 23 | + | ||
| 24 | + | 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](https://gitlab.com/freepascal.org/fpc/source/-/issues/39428). | |
| 25 | + | ||
| 26 | + | --- | |
| 27 | + | *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](/wiki-contact).* | |
| 28 | + | Category: Code Snippets |
