GetDiskFreeSpaceEx

Share your advanced PureBasic knowledge/code with the community.
high key
User
User
Posts: 23
Joined: Sun Jun 08, 2003 8:07 pm

GetDiskFreeSpaceEx

Post by high key »

Code updated For 5.20+
We don't have extralong integers to use the API-function directly, so I made a procedure biginteger2float, which can read the result of GetDiskFreeSpaceEx

Here's the code:

Code: Select all

Declare.f biginteger2float(adr,size)

;   
drive$=InputRequester("Drive info","Name of drive:","C")
If Len(drive$)=1: drive$=drive$+":": EndIf

Gosub testfree


End


testfree:


;reserve memory for 8-Byte-integers

bytesfreetocaller=AllocateMemory(8,0)
totalbytes=AllocateMemory(8,0)
totalfreebytes=AllocateMemory(8,0)       

GetDiskFreeSpaceEx_(@drive$, BytesFreeToCaller, TotalBytes, TotalFreeBytes)


fbytes.f=biginteger2float(bytesfreetocaller,8)
tbytes.f=biginteger2float(totalbytes,8)

MessageRequester("Info","Free bytes on drive "+drive$+Chr(13)+Chr(13)+StrF(fbytes)+" from "+StrF(tbytes),0)   

FreeMemory(bytesfreetocaller)
FreeMemory(totalbytes)
FreeMemory(totalfreebytes)


Return


Procedure.f biginteger2float(adr,size)
  
  multi.f=1
  v.f=0
  
  For i=0 To size
    b=PeekB(adr+i)
    If b<0: b+256: EndIf  ; signed to unsigned!
    
    v=v+b*multi     
    multi=multi*256    ; may get bigger than longinteger!
    
    ; test$=test$+Str(b)+"  "
  Next i
  
  ;  Debug test$
  
  ProcedureReturn v
  
EndProcedure   




GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Some time ago, i post a rountines to handle extra long integers (Add, sub, compare, mul, Div, and so on). Search for "LongLongs".

GPI
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

GPI: These routines are way too slow, to be really usefull for calculations.
Of course, for a thing like this, they are ok, because you don't calculate the
disk size over and over again.

Here's my little solution to that problem:

It does a little bit-shift, to divide the big value by 1024*1024, so it can fit
into a long again. The output is simply MegaBytes then.

Code: Select all

Structure int64
  Long1.l
  Long2.l
EndStructure

drive$ = "A:\"

; 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"

End
Timo
quidquid Latine dictum sit altum videtur
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Sorry High Key but your code doesn't work on Win2K.
Both displayed numbers are the same:
---------------------------
Info
---------------------------
Free bytes on drive C:

36893488147419103000.000000 from 36893488147419103000.000000
---------------------------
OK
---------------------------

Freak's code works fine...
anticro
New User
New User
Posts: 2
Joined: Thu Oct 14, 2004 11:43 am

Post by anticro »

Hey freak, thank you for the code! It works great! :)
NY152
User
User
Posts: 29
Joined: Sun May 14, 2006 12:33 am

Re: GetDiskFreeSpaceEx

Post by NY152 »

if EnableExplicit actived, an error occured on BytesFreeToCaller
.:NY152:.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: GetDiskFreeSpaceEx

Post by Thunder93 »

You need to declare all your variables...

Example;

Code: Select all

Define.INT64 BytesFreeToCaller, TotalBytes, TotalFreeBytes
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: GetDiskFreeSpaceEx

Post by Lunasole »

So old code ^^
Here is a bit updated @freak version which should work fine with disks up to 8388607 terabytes (yea, should be really enough for everybody)
Also don't know which OS showed "Insert drive" requester (Win98 or 2k?), on XP SP3 there is no such thing raised so I removed error mode switch too.

Code: Select all

EnableExplicit

Define.q BytesFreeToCaller, TotalBytes, TotalFreeBytes
Define drive$ = "d:\"

If GetDiskFreeSpaceEx_(@drive$, @BytesFreeToCaller, @TotalBytes, @TotalFreeBytes) = 0
	MessageRequester("","Drive not ready!",0)
	End
EndIf

Debug "Disk: " + drive$
Debug "Size: " + Str(TotalBytes) + " bytes"
Debug "Free: " + Str(TotalFreeBytes) + " bytes"
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: GetDiskFreeSpaceEx

Post by Kukulkan »

Windows only?
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: GetDiskFreeSpaceEx

Post by Thunder93 »

Windows specific, uses Win API.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: GetDiskFreeSpaceEx

Post by RSBasic »

Image
Image
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: GetDiskFreeSpaceEx

Post by Kukulkan »

User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: GetDiskFreeSpaceEx

Post by Shardik »

For MacOS Wilbert already posted this example.
Post Reply