How to get size of drive?

Just starting out? Need help? Post your questions and find answers here.
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

How to get size of drive?

Post by novablue »

Hello, i want to read out the total size or capacity of a drive letter, for example the disk size of drive c:\. how can i do that?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to get size of drive?

Post by RASHAD »

Hi
For Windows the simple approach

Code: Select all

Global Dim HDAvailableSpace.q(0)
Global Dim HDCapacity.q(0)
Global Dim HDFreeSpace.q(0)

Procedure GetDiskFreeSpace(drive$)
  SetErrorMode_(#SEM_FAILCRITICALERRORS)
  GetDiskFreeSpaceEx_(@drive$, HDAvailableSpace(), HDCapacity(), HDFreeSpace())
  SetErrorMode_(0)
EndProcedure

GetDiskFreeSpace("c:")
Debug "HD Capacity : "+Str(HDCapacity(0)/1024/1024/1024)+" GB"
Debug "Free Space : "+Str(HDFreeSpace(0)/1024/1024/1024)+" GB"
Debug "Used Space : "+Str((HDCapacity(0)-HDFreeSpace(0))/1024/1024/1024)+" GB"
Egypt my love
AZJIO
Addict
Addict
Posts: 1315
Joined: Sun May 14, 2017 1:48 am

Re: How to get size of drive?

Post by AZJIO »

If the disk is smaller than one gigabyte, its size will be clear.

Code: Select all

Define total_bytes.q

; Rounding to 3 digits after a period
Procedure.s FormatSizeDisk(Num.q)
	Protected.s snum = StrF(Num/1024)
	Protected.f total_gb = ValF(snum)
	total_gb = total_gb/1048576
	snum =StrF(total_gb,3)
	ProcedureReturn snum
EndProcedure

If GetDiskFreeSpaceEx_("C:", 0, @total_bytes, 0)
	Debug FormatSizeDisk(total_bytes) + " GB"
EndIf
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

Re: How to get size of drive?

Post by novablue »

Thank you that was exactly what i needed.
AZJIO
Addict
Addict
Posts: 1315
Joined: Sun May 14, 2017 1:48 am

Re: How to get size of drive?

Post by AZJIO »

RASHAD wrote: Sat Jan 08, 2022 12:56 am

Code: Select all

SetErrorMode_(#SEM_FAILCRITICALERRORS)
Does this code get rid of 4 messages that there is no disk in the Card Reader?
No computer to check. This problem exists in this program
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: How to get size of drive?

Post by ChrisR »

If needed, for formatting in GB, MB, KB,... I use the procedure SizeIt, found here

Code: Select all

Define Total_bytes.q, Total_FreeBytes.q

Procedure.s SizeIt(Value.q)
  Protected Unit=0, Byte.q, Pos.l, nSize.s
  Byte = Value
  While Byte >= 1024
    Byte / 1024 : Unit + 1
  Wend
  If Unit : nSize = StrD(Value/Pow(1024, Unit), 15) : Pos.l = FindString(nSize, ".") : Else : nSize = Str(Value) : EndIf
  If Unit : If Pos <  4 : nSize = Mid(nSize, 1, Pos+2) : Else : nSize = Mid(nSize, 1, Pos-1) : EndIf : EndIf
  ProcedureReturn nSize +Space(1)+ StringField("b ,KB,MB,GB,TB,PB", Unit+1, ",")
EndProcedure

If GetDiskFreeSpaceEx_("C:", 0, @Total_Bytes, @Total_FreeBytes)
  Debug "Space available: " + SizeIt(Total_Bytes) + " - Used space: "  + SizeIt(Total_Bytes - Total_FreeBytes) + " - Free space: "  + SizeIt(Total_FreeBytes) 
EndIf
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: How to get size of drive?

Post by BarryG »

AZJIO wrote: Tue Jan 18, 2022 7:56 pmDoes this code get rid of 4 messages that there is no disk in the Card Reader
Yes. I've been using that forever to prevent Windows showing messages about missing media in the drives.
AZJIO
Addict
Addict
Posts: 1315
Joined: Sun May 14, 2017 1:48 am

Re: How to get size of drive?

Post by AZJIO »

ChrisR wrote: Tue Jan 18, 2022 10:02 pm If needed, for formatting in GB, MB, KB,... I use the procedure SizeIt, found here
In that function I have an alignment to the right and the size is also determined visually by the protrusion of the numbers to the left.
To determine the size, I use 3 digit numbers and a prefix so that the width is the same. I use it in this program.

Code: Select all

Procedure.s _ShortFileSize(Bytes.q)
	Protected res.s
	Select Bytes
		Case 10995116277760 To 109951162777600 ; 10 - 100 TB
			res = StrD(Round(Bytes / 109951162777.6, #PB_Round_Nearest)/10.0) + " TB"
		Case 1000000000000 To 10995116277759 ; 1000 GB - 10 TB
			res = StrD(Round(Bytes / 10995116277.76, #PB_Round_Nearest)/100.0) + " TB"
		Case 107374182400 To 999999999999 ; 100 - 999 GB
			res = StrD(Round(Bytes / 1073741824, #PB_Round_Nearest)) + " GB"
		Case 10737418240 To 107374182399 ; 10 - 100 GB
			res = StrD(Round(Bytes / 107374182.4, #PB_Round_Nearest)/10.0) + " GB"
		Case 1000000000 To 10737418239 ; 1000 MB - 10 GB
			res = StrD(Round(Bytes / 10737418.24, #PB_Round_Nearest)/100.0) + " GB"
		Case 104857600 To 999999999 ; 100 - 999 MB
			res = StrD(Round(Bytes / 1048576, #PB_Round_Nearest)) + " MB"
		Case 10485760 To 104857599 ; 10 - 100 MB
			res = StrD(Round(Bytes / 104857.6, #PB_Round_Nearest)/10.0) + " MB"
		Case 1000000 To 10485759 ; 1000 KB - 10 MB
			res = StrD(Round(Bytes / 10485.76, #PB_Round_Nearest)/100.0) + " MB"
		Case 102400 To 999999 ; 100 - 999 KB
			res = StrD(Round(Bytes / 1024, #PB_Round_Nearest)) + " KB"
		Case 10240 To 102399 ; 10 - 100 KB
			res = StrD(Round(Bytes / 102.4, #PB_Round_Nearest)/10.0) + " KB"
		Case 1000 To 10239 ; 1000 B - 10 KB
			res = StrD(Round(Bytes / 10.24, #PB_Round_Nearest)/100.0) + " KB"
		Case 0 To 999
			res = StrD(Bytes )+ " B"
	EndSelect
	ProcedureReturn res
EndProcedure


Debug _ShortFileSize(100000)
Debug _ShortFileSize(10000000)
Debug _ShortFileSize(1000000000)
Debug _ShortFileSize(100000000000)
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: How to get size of drive?

Post by ChrisR »

AZJIO wrote: Wed Jan 19, 2022 11:46 pm In that function I have an alignment to the right and the size is also determined visually by the protrusion of the numbers to the left.
To determine the size, I use 3 digit numbers and a prefix so that the width is the same. I use it in this program.
Good indeed, to respect the alignment as in your use in File scanner :)
The SizeIt() procedure can also be useful for formatting a single value without the need for alignment.
Post Reply