Getting Free Disk Space
Posted: Thu Sep 13, 2012 6:07 pm
Hello,
How can I get free disk space for any particular drive using PB?
Thanks,
Michael
How can I get free disk space for any particular drive using PB?
Thanks,
Michael
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
EnableExplicit
Define lpFreeBytesAvailableToCaller.q, lpTotalNumberOfBytes.q, lpTotalNumberOfFreeBytes.q
GetDiskFreeSpaceEx_("C:", @lpFreeBytesAvailableToCaller, @lpTotalNumberOfBytes, @lpTotalNumberOfFreeBytes)
Debug lpFreeBytesAvailableToCaller
Debug lpTotalNumberOfBytes
Debug lpTotalNumberOfFreeBytes
Code: Select all
Procedure.i GetDiskFreeSpace (drive$, *UserFreeSpace, *TotalSpace, *FreeSpace = #Null)
; [DESC]
; Return the free space for the specified local ("C:\") or networked ("\\ARCHIMEDE\SHARE\") volume.
;
; [INPUT]
; drive$ : Points to a string that contains the root directory of the volume to be described.
;
; [OUTPUT]
; *UserFreeSpace : Pointer to a quad, it will contain the free space available for the user of this process (in case of quotas).
; *TotalSpace : Pointer to a quad, it will contain the total space (free + used) for the volume.
; *FreeSpace : Pointer to a quad, it will contain the free space available for the volume.
;
; [RETURN]
; 1 if OK, else 0.
;
; [NOTES]
; If drive$ is empty, the root of the current directory is used.
; if *FreeSpace is #Null it will be skipped (it's optional).
If drive$ = ""
drive$ = Left(GetCurrentDirectory(),3)
EndIf
If GetDiskFreeSpaceEx_(drive$, *UserFreeSpace, *TotalSpace, *FreeSpace)
ProcedureReturn 1
EndIf
ProcedureReturn 0
EndProcedure
Define.q UserFreeSpace, TotalSpace, FreeSpace
Debug GetDiskFreeSpace ("D:\", @UserFreeSpace, @TotalSpace, @FreeSpace)
Debug UserFreeSpace
Debug TotalSpace
Debug FreeSpace
Debug GetDiskFreeSpace ("", @UserFreeSpace, @TotalSpace)
Debug UserFreeSpace
Debug TotalSpace
Code: Select all
; freak: http://www.purearea.net/pb/CodeArchiv/Windows_System/Hardware/GetDiskFreeSpace.pb
Structure int64
Long1.l
Long2.l
EndStructure
drive$ = "C:\"
; this prevents the 'please insert drive' requester.
; GetDiskFreeSpaceEx_() will just return 0 if the drive is not avaiable,
; without a prompt to the user :
SetErrorMode_(#SEM_FAILCRITICALERRORS)
If GetDiskFreeSpaceEx_(@drive$, BytesFreeToCaller.int64, TotalBytes.int64, TotalFreeBytes.int64) = 0
MessageRequester("", "Drive not ready!", 0)
End
EndIf
; reset the error behaviour
SetErrorMode_(0)
; calculate sizes in mb.
TotalMB = ((TotalBytes\Long1 >> 20) & $FFF) | (TotalBytes\Long2 << 12)
FreeMB = ((TotalFreeBytes\Long1 >> 20) & $FFF) | (TotalFreeBytes\Long2 << 12)
Debug "Disk : " + drive$
Debug "Size : " + Str(TotalMB) + " Mb"
Debug "Free : " + Str(FreeMB) + " Mb"
Code: Select all
; PB: http://www.purebasic.fr/english/viewtopic.php?p=129208
; r=GetDiskSpace("c : ", "t") ; Get number of total bytes on C :
; r=GetDiskSpace("c : ", "f") ; Get number of free bytes on C :
; r=GetDiskSpace("c : ", "u") ; Get number of used bytes on C :
;
; r = -1 on failure (eg. drive not found, or bad type$ param).
Procedure.q GetDiskSpace(drive$, type$)
value.q = - 1
If GetDiskFreeSpaceEx_(drive$, @free.q, @total.q, 0)
Select LCase(type$)
Case "t" : value = total
Case "f" : value = free
Case "u" : value = total - free
Default : value = - 1
EndSelect
EndIf
ProcedureReturn value
EndProcedure
drive.s = "C:"
Debug Str(GetDiskSpace(drive, "t")) + " Total"
Debug Str(100 * GetDiskSpace(drive, "u") / GetDiskSpace(drive, "t")) + " % Used"
Debug Str(100 * GetDiskSpace(drive, "f") / GetDiskSpace(drive, "t")) + " % Free"
Code: Select all
Prototype StrFormatByteSize64(num.q, *buffer, length.l)
Global StrFormatByteSize64.StrFormatByteSize64
Procedure.i shlwapi_LoadDLL()
Protected hDLL.i
hDLL = OpenLibrary(#PB_Any, "shlwapi.dll")
If hDLL <> 0
StrFormatByteSize64 = GetFunction(hDLL, "StrFormatByteSize64A")
ProcedureReturn hDLL
EndIf
ProcedureReturn #False
EndProcedure : shlwapi_LoadDLL()
Procedure.s StrFormatSize(num.q)
Protected *string, buf.s = Space(255)
*string = StrFormatByteSize64(num, @buf, 255)
If *string
ProcedureReturn PeekS(*string, -1, #PB_Ascii)
EndIf
EndProcedure
Debug StrFormatSize(1000)
Debug StrFormatSize(10000)
Debug StrFormatSize(1000000)
Debug StrFormatSize(100000000)
Debug StrFormatSize(10000000000)
Debug StrFormatSize(10000000000000)
Debug StrFormatSize(10000000000000000)
Code: Select all
Procedure.s SizeIt(Value.q)
Protected unit.b=0, byte.q, 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+" "+StringField("bytes,KB,MB,GB,TB,PB", unit+1, ",")
EndProcedure
Debug SizeIt(1000)
Debug SizeIt(10000)
Debug SizeIt(1000000)
Debug SizeIt(100000000)
Debug SizeIt(10000000000)
Debug SizeIt(10000000000000)
Debug SizeIt(10000000000000000)
Code: Select all
TotalBytes.q
GetDiskFreeSpaceEx_(@drive$, BytesFreeToCaller.int64, @TotalBytes, TotalFreeBytes.int64)
Code: Select all
Structure int64
Long1.l
Long2.l
EndStructure
drive$ = "C:\"
SetErrorMode_(#SEM_FAILCRITICALERRORS)
TotalBytes.q
If GetDiskFreeSpaceEx_(@drive$, BytesFreeToCaller.int64, @TotalBytes, TotalFreeBytes.int64) = 0
MessageRequester("", "Drive not ready!", 0)
End
EndIf
SetErrorMode_(0)
Debug "My C:\ Drive Total Size (in bytes): "+Str(TotalBytes)
Procedure.s SizeIt(Value.q)
Protected unit.b=0, byte.q, 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+" "+StringField("bytes,KB,MB,GB,TB,PB", unit+1, ",")
EndProcedure
Debug "My C:\ Drive Total Size (..converted over from bytes): "+SizeIt(TotalBytes)
Code: Select all
Drive$ = "C:\"
SetErrorMode_(#SEM_FAILCRITICALERRORS)
Define.q BytesFreeToCaller, TotalBytes, TotalFreeBytes
If GetDiskFreeSpaceEx_(@Drive$, @BytesFreeToCaller, @TotalBytes, @TotalFreeBytes) = 0
MessageRequester("", "Drive not ready!", 0)
End
EndIf
SetErrorMode_(0)
Debug "My C:\ Drive Total Size (in bytes): "+Str(TotalBytes)
Procedure.s SizeIt(Value.q)
Protected unit.b=0, byte.q, 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+" "+StringField("bytes,KB,MB,GB,TB,PB", unit+1, ",")
EndProcedure
Debug "My C:\ Drive Total Size (..converted over from bytes): "+SizeIt(TotalBytes)
Debug "BytesFreeToCaller: "+SizeIt(BytesFreeToCaller)
Debug "TotalFreeBytes: "+SizeIt(TotalFreeBytes)