Silverfrost Forums

Welcome to our forums

WinAPI function of free disk space left

21 Jul 2024 10:26 #31434

Is there such function? Good would be to create with Clearwin such a reminder that will warn you that the hard drives free space limit is approaching.

Workarounds like 'Dir >file' and then looking into the file is too slow if the directory has thousands of files

Or similar function for Linux?

22 Jul 2024 12:45 #31436

To speed up you could try something like:

'Dir zz.* >file'

( although I am not familiar with correct syntax to select the appropriate drive and directory )

22 Jul 2024 6:44 #31437

If you right click on a hard drive in File Explorer then it gives you the used and free space immediately. The same is true for the Settings (System > Storage > Disks & volumes).

This suggests that the hard drive values are kept somewhere in the registry but I don't know where to find them.

If you right click on a folder in File Explorer then it starts to calculate the sizes and this can take a while. This implies that the sizes are accessed and summed dynamically.

The FTN95 subroutines FILES@ or FILES8@ (for example) could be used to get file sizes for summing but how long would this take to compute?

At a lower level (in C rather than Fortran) one could call FindFirstFile and FindNextFile (for example) recursively and sum the sizes from the returned data structures. Again this might not be fast enough for your purposes.

22 Jul 2024 10:19 #31438

Paul,

'If you right click on a hard drive in File Explorer then it gives you the used and free space immediately'

This would be a statistic readily obtained from the file availability table statistics of the selected logical disk. = ( total_sectors - sectors_allocated ) * sector_size_bytes

Is there any WINAPI that provides these values.

( there is for memory availability !ftn95 stdcall GlobalMemoryStatusEx 'GlobalMemoryStatusEx'(REF):logical ! logical, external :: GlobalMemoryStatusEx )

FILES@ does not appear to have access to total disk size so might not help.

22 Jul 2024 12:54 #31439

Well, Dan, If you are afraid that you are approaching your disk limit with the general run of things in your work, you can keep a check by interrogating 'Properties' for any disk in the File Explorer. Provided you do this on a regular basis, it gives you enough time to go out and buy a larger disk, and install it in your PC. The costs involved, and time taken, are a fraction of the inconvenience of running up against a limit and causing a crash.

If it is a matter of a temporary file storage issue, then keep a drive specifically for those temporary files. The shop I use has 5Tb SSDs and spinning disks, the latter only 25% of the cost of the former, and even those are priced at a fraction of a day's consulting charge-out rate.

Eddie

22 Jul 2024 2:28 #31440

Here is some code to try...

program main
c_external GetDiskSpaceInformation 'GetDiskSpaceInformationA'(INSTRING,REF):LOGICAL
type DISK_SPACE_INFORMATION
  sequence
  integer(4) ActualTotalAllocationUnits;
  integer(4) ActualAvailableAllocationUnits;
  integer(4) ActualPoolUnavailableAllocationUnits;
  integer(4) CallerTotalAllocationUnits;
  integer(4) CallerAvailableAllocationUnits;
  integer(4) CallerPoolUnavailableAllocationUnits;
  integer(4) UsedAllocationUnits;
  integer(4) TotalReservedAllocationUnits;
  integer(4) VolumeStorageReserveAllocationUnits;
  integer(4) AvailableCommittedAllocationUnits;
  integer(4) PoolAvailableAllocationUnits;
  integer(3) SectorsPerAllocationUnit;
  integer(3) BytesPerSector;
end type
type(DISK_SPACE_INFORMATION)::diskSpaceInfo
logical res
diskSpaceInfo = DISK_SPACE_INFORMATION(0,0,0,0,0,0,0,0,0,0,0,0,0)
res = GetDiskSpaceInformation('C:\', diskSpaceInfo)
print*, diskSpaceInfo
end program

res returns FALSE (which is wrong) but the data structure is filled.

22 Jul 2024 6:02 #31442

Thanks all for suggestions. Why i need this subroutine is that no matter how many other places will tell you about the size what is left (and each of 10-20 open Total Commanders, or Double Commanders on top clearly write the number of free space left ) you always will miss this ball. The idea is to make warning when it happen, flash the light, generate sound, to wake you up and/or just pause the run before the crash happen. In 4+ decades i run the codes which always generate a lot of large files i probably lost a year just on that 'disk full' crashes because the night run was not complete or continuation file was damaged pushing me to run again.

Paul, This is what i was looking for, many thanks.

I got some problem though which is not related to the code, may be you can suggest where to look for the source: code works on true Windows computer but complains on emulated Windows one (in Linux) by unknown reason. I never had any problems like that before, all Windows stuff worked like i am in true Windows. The error is : Call to missing routine_GetDiskSpaceInformationA. Is it possible that my emulator uses probably the oldest version of WIndows 10 ? I remember 5 or more years ago in Windows were some changes of this sort

23 Jul 2024 6:11 #31443

Dan

This function requires Windows 10 or 11.

https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskspaceinformationw

You could try using the UNICODE form: GetDiskSpaceInformationW but you would need to change the first argument as well.

24 Jul 2024 10:20 #31445

Paul, thanks, unfortunately looks like the Windows used by my relatively old version of WINE 6.0.3 is too old too. Will experiment with newer WINE and newer Windows later.

/* By the way WINE is great emulator of Windows for Linux (literally ideal one, much more convenient and stable than for example VirtualBox ) but is very picky at installation time. I hope Android and Chrome will teach entire Linux community how desktop Linux has to be implemented (all 1000 installations/uninstalls by billions people were done in one click and no single problem were with system updates in decade)

Please login to reply.