Page 1 of 1

How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 10:42 am
by hoangdiemtinh
I am new to PB.
My primary language is not US/UK. I am using Google Translate.

I found a topic about "Find all drive letters that are Fixed Drives": https://www.purebasic.fr/english/viewtopic.php?t=26763
Now, I need to find (scan) the drive letter with the largest free space.

Thanks.

Re: How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 10:59 am
by AZJIO
GetDiskFreeSpaceEx_()

Re: How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 11:05 am
by hoangdiemtinh
Thank AZJIO.

But, mean:
Filter out drive letter that have more free space than other drives.

Re: How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 12:25 pm
by AZJIO

Code: Select all

max = 0
ForEach DiskSize()
	If DiskSize() > max
		max = DiskSize()
	EndIf
Next
https://www.purebasic.fr/english/viewtopic.php?t=78880

Code: Select all

EnableExplicit
Define NewList OccupiedSize.q()
Define lpFreeBytesAvailable.q
Define lpTotalNumberOfBytes.q
Define OccupiedSize.q
Define max.q
GetDiskFreeSpaceEx_("C:", @lpFreeBytesAvailable, @lpTotalNumberOfBytes, 0)
OccupiedSize = lpTotalNumberOfBytes - lpFreeBytesAvailable

AddElement(OccupiedSize())
OccupiedSize() = OccupiedSize

GetDiskFreeSpaceEx_("D:", @lpFreeBytesAvailable, @lpTotalNumberOfBytes, 0)
OccupiedSize = lpTotalNumberOfBytes - lpFreeBytesAvailable

AddElement(OccupiedSize())
OccupiedSize() = OccupiedSize

max = 0
ForEach OccupiedSize()
	Debug OccupiedSize()
	If OccupiedSize() > max
		max = OccupiedSize()
	EndIf
Next
Debug "max = " + max
Debug StrF(ValF(StrF(max / 1024)) / 1048576, 3) + " Gb"

Re: How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 12:51 pm
by hoangdiemtinh
I tested:
- Drive C:\ has 25GB free space.
- Drive D:\ has 50GB free space.
Running the code you gave, I get 25GB as a result.
(It should be drive D:\)

Re: How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 12:56 pm
by spikey
Enumerating drives a slightly different way:

Code: Select all

Structure DRIVESPACE
  Root.S
  FreeBytesAvailable.Q
  TotalNumberOfBytes.Q
  TotalNumberOfFreeBytes.Q
EndStructure

Define.I CharsReturned, BytesReturned
Define AllDrives.s = Space(255)
NewList Drives.DRIVESPACE()

CharsReturned = GetLogicalDriveStrings_(255, @AllDrives)
BytesReturned = (CharsReturned * 2) - 1
For x = 0 To BytesReturned Step 8
  AddElement(Drives())
  Drives()\Root = PeekS(@AllDrives + x, #PB_Unicode)  
  GetDiskFreeSpaceEx_(@Drives()\Root, @Drives()\FreeBytesAvailable, 
                      @Drives()\TotalNumberOfBytes, @Drives()\TotalNumberOfFreeBytes)
Next x

SortStructuredList(Drives(), #PB_Sort_Descending, OffsetOf(DRIVESPACE\FreeBytesAvailable), #PB_Quad)
FirstElement(Drives())
Debug "There are " + ListSize(Drives()) + " drives available."
Debug "Drive " + Drives()\Root + " has the most free space available " + Drives()\FreeBytesAvailable + " bytes." 
Debug ""
ForEach Drives()
  Debug Drives()\Root + " has " + Drives()\FreeBytesAvailable
Next Drives()

Re: How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 1:35 pm
by hoangdiemtinh
Thank @spikey.

My problem solved.

Re: How to find the drive with the largest free space ?

Posted: Mon May 20, 2024 4:15 pm
by AZJIO

Code: Select all

EnableExplicit

Structure info
	Drive.s
	FreeBytesAvailable.q
EndStructure

Define max.info

Procedure GetDrives(List Drive.info())
	Protected i, drives_avail
	drives_avail = GetLogicalDrives_()

	For i = 0 To 25
		If drives_avail >> i & 1
			If AddElement(Drive())
				Drive()\Drive = Chr(i + 65)
			EndIf
		EndIf
	Next
EndProcedure

Define NewList Drive.info()
GetDrives(Drive())
ForEach Drive()
	GetDiskFreeSpaceEx_(Drive()\Drive + ":", @Drive()\FreeBytesAvailable, 0, 0)
	; Debug Drive()\Drive
	; Debug Drive()\FreeBytesAvailable
	If Drive()\FreeBytesAvailable > max
		max\FreeBytesAvailable = Drive()\FreeBytesAvailable
		max\Drive = Drive()\Drive
	EndIf
Next
Debug "—————————"
Debug "maxSize = " + max\FreeBytesAvailable + "  (" + StrF(ValF(StrF(max\FreeBytesAvailable / 1024)) / 1048576, 3) + " Gb)"
Debug "maxFreeDrive = " + max\Drive