How to find the drive with the largest free space ?

Everything else that doesn't fall into one of the other PB categories.
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

How to find the drive with the largest free space ?

Post 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.
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

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

Post by AZJIO »

GetDiskFreeSpaceEx_()
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

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

Post by hoangdiemtinh »

Thank AZJIO.

But, mean:
Filter out drive letter that have more free space than other drives.
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

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

Post 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"
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

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

Post 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:\)
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
User avatar
spikey
Enthusiast
Enthusiast
Posts: 769
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

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

Post 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()
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

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

Post by hoangdiemtinh »

Thank @spikey.

My problem solved.
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
AZJIO
Addict
Addict
Posts: 2191
Joined: Sun May 14, 2017 1:48 am

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

Post 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
Post Reply