Drive Space

Does anybody know how to find out the space remaining on a drive in Widows without using a plugin…?

Thanks in Advance.

'https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexw

Declare Function GetDiskFreeSpaceExW Lib "Kernel32.dll" Alias "GetDiskFreeSpaceExW" ( _
lpDirectoryName As WString, _
ByRef lpFreeBytesAvailableToCaller As UInt64, _
ByRef lpTotalNumberOfBytes As UInt64, _
ByRef lpTotalNumberOfFreeBytes As UInt64 _
) As Int32

Dim FreeBytesAvailableToCaller As UInt64 'available to the user
Dim TotalNumberOfBytes As UInt64 'disk size
Dim TotalNumberOfFreeBytes As UInt64 'on the disk
Dim ok As Int32 'if this is 0 after GetDiskFreeSpaceExW, then it failed

ok = GetDiskFreeSpaceExW("c:\", FreeBytesAvailableToCaller, TotalNumberOfBytes, TotalNumberOfFreeBytes)

system.DebugLog(str(FreeBytesAvailableToCaller / 1073741824) + " GB free")
2 Likes

Thanks Julian, yours is better than what I’ve been using. I’ll post mine since it is also a way to find all the drives.

Var s As New Shell
s.TimeOut = 5000 ' 5 second timeout (default of 2 sometimes isn't enough)
s.Execute("wmic LogicalDisk Get DeviceID,FreeSpace")
' s.Result will look list this. White space is multiple spaces, CRLF at end of line.
' Some drives may not have info (no media.) Number is bytes so divide to get GB.
'   DeviceID  FreeSpace
'   C:        1451803942912
'   D:
'   E:        4822050590720
'   F:        716923432960

Thank you both for your replies, now all I have to do is study them and learn how they work (Eric’s seem firly easy to understand) less so.
Is very good of you both to take the time to help me.

Ian.

Hi Again, Took me a couple of minutes to work it out but got it sorted so once again thank yop very much for the help.

Ian

I think you should be careful with the timeout on this. If you are releasing this for other windows machines to use, be advised that if wmic is being run for the first time on a machine your timeout of 5 seconds may be too short and not return the results you were expecting.

Running it for the first time on a machine can cause it to say:

Please wait while WMIC is being installed…

Be aware that MS has deprecated WMIC starting in 21H1 and you will need to call WMI through PowerShell

https://docs.microsoft.com/en-us/windows/deployment/planning/windows-10-deprecated-features