Page 1 of 1

GetDiskFreeSpaceEx

Posted: Wed Dec 31, 2003 2:30 pm
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   





Posted: Wed Dec 31, 2003 2:48 pm
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

Posted: Wed Dec 31, 2003 5:01 pm
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

Posted: Wed Dec 31, 2003 5:40 pm
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...

Posted: Sun Nov 14, 2004 6:38 pm
by anticro
Hey freak, thank you for the code! It works great! :)

Re: GetDiskFreeSpaceEx

Posted: Mon Jul 11, 2016 9:00 pm
by NY152
if EnableExplicit actived, an error occured on BytesFreeToCaller

Re: GetDiskFreeSpaceEx

Posted: Mon Jul 11, 2016 9:53 pm
by Thunder93
You need to declare all your variables...

Example;

Code: Select all

Define.INT64 BytesFreeToCaller, TotalBytes, TotalFreeBytes

Re: GetDiskFreeSpaceEx

Posted: Tue Jul 12, 2016 4:53 am
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"

Re: GetDiskFreeSpaceEx

Posted: Tue Jul 12, 2016 7:32 am
by Kukulkan
Windows only?

Re: GetDiskFreeSpaceEx

Posted: Tue Jul 12, 2016 11:17 am
by Thunder93
Windows specific, uses Win API.

Re: GetDiskFreeSpaceEx

Posted: Tue Jul 12, 2016 12:01 pm
by RSBasic

Re: GetDiskFreeSpaceEx

Posted: Tue Jul 12, 2016 1:22 pm
by Kukulkan

Re: GetDiskFreeSpaceEx

Posted: Tue Jul 12, 2016 1:39 pm
by Shardik
For MacOS Wilbert already posted this example.