November 25, 2020, 06:06:42 AM

Author Topic: [bb] Check Free Disk Space by Danny [ 1+ years ago ]  (Read 606 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Check Free Disk Space by Danny [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Check Free Disk Space
Author : Danny
Posted : 1+ years ago

Description : The routine below is based on Yasha's example in the forum.
See here: <a href="../Community/postsea57.html?topic=97357" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=97357[/url]

It may help you when you want to check that there's enough space left on a given device before saving an amount of data to it. Since B3D doesn't support long integers, it can only check up to a specific max of 2.1 Gb being free - or not.
Also note that you should supply the path you intend to write data to, not just the drive, since some windows configurations might use user-based disk quotas. See here for more info on that: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx" target="_blank">http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937%28v=vs.85%29.aspx[/url]

Enjoy.

Danny


Code :
Code: BlitzBasic
  1. ; Add to Kernal32.decls:
  2. ;
  3. ; .lib "Kernel32.dll"
  4. ; API_GetDiskFreeSpaceEx%(lpDirectoryName$, lpFreeBytesAvailable*, lpTotalNumberOfBytes*, lpTotalNumberOfFreeBytes*) : "GetDiskFreeSpaceExA"
  5.  
  6.  
  7. Type LongInt
  8.         Field a%, b%
  9. End Type
  10.  
  11. Function HasFreeSpace%(path$, minFreeBytes%)
  12.        
  13.         ;# Returns TRUE (1), FALSE(0) or -1 when path not found/accessible/mounted
  14.         ;
  15.         ;# Max bytes that can be tested is 2,147,483,647 bytes (=2.1Gb = 2048 Mb)
  16.         ;# Thus always returns TRUE if more than 2.1Gb is free!
  17.        
  18.         If minFreeBytes < 0 Then minFreeBytes = ((2048 *1024) *1024) -1
  19.        
  20.         GFS_out_FABytes.LongInt = New LongInt
  21.         GFS_out_TotBytes.LongInt = New LongInt
  22.         GFS_out_TFBytes.LongInt = New LongInt
  23.        
  24.         Local ret% = False
  25.        
  26.         If API_GetDiskFreeSpaceEx(path, GFS_out_FABytes, GFS_out_TotBytes, GFS_out_TFBytes)
  27.                
  28.                 If GFS_out_FABytes <> 0
  29.                         ; when B <> 0 then MORE than 4.2Gb free
  30.                         ret = True
  31.                 ElseIf (GFS_out_FABytes = 0) And (GFS_out_FABytesa < 0)
  32.                         ; when B = 0, A < 0, MORE than 2.1Gb free
  33.                         ret = True
  34.                 ElseIf GFS_out_FABytesa > minFreeBytes Then
  35.                         ; A contains remaining bytes free
  36.                         ret = True
  37.                 Else
  38.                         ; NOT ENOUGH SPACE FREE!
  39.                         ret = False
  40.                 EndIf
  41.                
  42.         Else
  43.                 ; Path not found, drive doesn't exist or media not mounted
  44.                 ret = -1
  45.                
  46.         EndIf
  47.        
  48.         ; purge
  49.         Delete GFS_out_FABytes
  50.         Delete GFS_out_TotBytes
  51.         Delete GFS_out_TFBytes
  52.         ; return
  53.         Return ret
  54.        
  55. End Function
  56.  
  57.  
  58. ; EXAMPLE USE
  59.  
  60. Local drive$ = "C:"
  61.  
  62. Local megabyte% = 100
  63. Local bytes% = ((megabyte *1024) *1024)
  64.  
  65. Select HasFreeSpace(drive, bytes)
  66.         Case True
  67.                 Print " "+drive+" has more than "+megabyte+" Mb free ("+bytes+" bytes)"
  68.         Case False
  69.                 Print " "+drive+" DOES NOT have "+megabyte+" Mb free ("+bytes+" bytes) !"
  70.         Case -1
  71.                 Print " "+drive+" not accessible, found or mounted!"
  72. End Select
  73.  
  74. Print "" : Print " <any key>"
  75. WaitKey
  76. End


Comments :


Yasha(Posted 1+ years ago)

 This version has a (kinda) memory leak - you're creating the three LongInt objects anew each time but not freeing them afterwards (this is why in the original example I cached them in a global). If you use this function more than once, it will start to waste space.You don't have to store them between calls, if you want to keep the surrounding code tidier, but in that case you do need to have a corresponding Delete section at the bottom.(I say "kinda" because objects in B3D created with New are never truly unreachable - you can quickly kill 'em all with "Delete Each LongInt". However, unless you do something like this somewhere in your program it's effectively the same as a leak.)


Danny(Posted 1+ years ago)

 Ay, yes I thought about then when removing the Globals, but forgot to put the Deletes in. The code above is updated so should be safe now.Thanks for pointing that out ;)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal