B4J Library jDiskSpace

Hope this is helpful for others.

This library I created to allow you to view the disk space for drive letters C: D: E: F:

Hard drive(s)
CD(s)
Flash Media
are all supported provided they are in one of the above drive letters. C,D,E,F
***NOTE The number returns in "MB" so you will have to convert it to GB if you would like.

C TotalSpace
C FreeSpace
C UsableSpace
C UsedSpace


***COPY both files to your additional libraries folder and you are ready to go***

Enjoy.
 

Attachments

  • jDiskSpace.zip
    1.9 KB · Views: 664
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Thanks for this library. It works well.

Is there any reason why it is limited to only letters from C to F?
I was wondering if it would be better to call the methods this way instead:
B4X:
Dim ds As jDiskSpace
ds.getFreeSpace("C:")
ds.getTotalSpace("G:")
'...

Also, could it be possible to add a DriveList method, which would return a list of drives:
B4X:
ds.GetDrive("ALL")
ds.GetDrive("CDROM")
ds.GetDrive("REMOVABLE")
ds.GetDrive("NETWORK")
ds.GetDrive("FIXED")
'...

That way we could iterate through the drives:
B4X:
For Each Drive As String In ds.GetDrives("FIXED")
    Log(ds.GetSpace(Drive))
Next

Thanks.
 

maxer73

Member
Thanks giga, the library works fine, but in this way you have many methods and only drive letters C: D : E: F:

If you example has 1 or more USB Splitter and more pendrive or HD, maybe need letters G: H: I: L: etc .... ....

I think the best way would be to enumerate each drive, so
you can get letters from A : (floppy) to Z: (last unit available in the system)

So for example, you can control the drivers by INDEX :
B4X:
' Read Free space on all Drivers by index
Dim jDS As jDiskSpace
For Each driveIndex As Byte = jDS.GetNumberOfDrivers()
  Log ("Disk Index:" & driveIndex & "      Free:" & jDS.getFreeSpaceByIndex(driveIndex) &_
                                    "     Total:" & jDS.getTotalSpaceByIndex(driveIndex) &_
                                    "   Useable:" & jDS.getUseableSpaceByIndex(driveIndex) &_
                                    "      Used:" & jDS.getUsedSpaceByIndex(driveIndex))
Next

Or with simple For loop (same by INDEX):
B4X:
' Same but using simple For loop
Dim jDS As jDiskSpace
Dim driveIndex as byte

For driveIndex = 0 to (jDS.GetNumberOfDrivers()-1)
   Log ("Disk Index:" & driveIndex & "      Free:" & jDS.getFreeSpaceByIndex(driveIndex) &_
                                     "     Total:" & jDS.getTotalSpaceByIndex(driveIndex) &_
                                     "   Useable:" & jDS.getUseableSpaceByIndex(driveIndex) &_
                                     "      Used:" & jDS.getUsedSpaceByIndex(driveIndex))
Next

or by NAME :
B4X:
Dim jDS As jDiskSpace
Log ("Free disk space on the floppy is " & jDS.getFreeSpaceByLetter("A")) ' should always be the first letter, but as in modern PCs
                                                                          ' no longer used the floppy, maybe to A: drive is assigned to another

' Read infos on Drive C: by LETTER
If jDS.getDriveExistsByLetter("C") then          ' check if drive C exists
   Log ("Free disk space on C: is " & jDS.getFreeSpaceByLetter("C:"))
   Log ("Total disk space on C: is " & jDS.getTotalSpaceByLetter("C:"))
   Log ("Useable space on drive C: is " & jDS.getUseableSpaceByLetter("C:"))
   Log ("Used disk space on C: is " & jDS.getUsedSpaceByLetter("C:"))
Next

' Read infos on drive D: by LETTER
If jDS.getDriveExistsByLetter("D") then           ' check if drive D exists
   Log ("Free space on D: is " & jDS.getFreeSpaceByLetter("D:"))
   Log ("Total disk space on D: is " & jDS.getTotalSpaceByLetter("D:"))
   Log ("Useable space on D: is " & jDS.getUseableSpaceByLetter("D:"))
   Log ("Used space on D: is " & jDS.getUsedSpaceByLetter("D:"))
Next

' Read Free space on PenDrive by LETTER
If jDS.getDriveExistsByLetter("E:") then Log ("Free space on PenDrive is " & jDS.getFreeSpaceByLetter("E:"))

' ............. WE CAN CONTINUE WITH DRIVER UNITS ( FROM A: TO Z: ) ..............
' ...............................................................................
' ...............................................................................

jmon is absolutely right , I agree with him ... in this way would be much better and you could iterate through the driver units in a For loop or For Each, While, etc ... ...

It would be a nice thing to also be able to differentiate the types of units as jmon says, so you know if a unit is a PenDrive, HD, CDROM, etc ... ...
This is possible Because The system I can read infos on Units classified ... (see Resource Explorer)

Sub GetDriveType(Byte Index, String Type) as Boolean ' test type of Drive

That return boolean know this possible example:
Letter can be "A:", "B:", "C:" etc ... ...
Type can be "ALL", "CDROM", "REMOVABLE", "NETWORK", "FIXED" etc ... ...

B4X:
' Write all infos on FIXED Units (like fixed HD)
Dim jDS As jDiskSpace
For Each driveIndex As Byte = jDS.GetNumberOfDrivers()
   If jDs.GetDriveType(driveIndex,"FIXED")
     Log ("Found FIXED Drive      Drive infos       Free:" & jDS.getFreeSpaceByIndex(driveIndex) &_
                                             "     Total:" & jDS.getTotalSpaceByIndex(driveIndex) &_
                                             "   Useable:" & jDS.getUseableSpaceByIndex(driveIndex) &_
                                             "      Used:" & jDS.getUsedSpaceByIndex(driveIndex))
   Next
Next

B4X:
' Write all infos on REMOVABLE Units (like removable HD, PenDrive etc...)
Dim jDS As jDiskSpace
For Each driveIndex As Byte = jDS.GetNumberOfDrivers()
   If jDs.GetDriveType(driveIndex,"REMOVABLE")
     Log ("Found REMOVABLE Drive      Drive infos      Free:" & jDS.getFreeSpaceByIndex(driveIndex) &_
                                                "     Total:" & jDS.getTotalSpaceByIndex(driveIndex) &_
                                                "   Useable:" & jDS.getUseableSpaceByIndex(driveIndex) &_
                                                "      Used:" & jDS.getUsedSpaceByIndex(driveIndex))
   Next
Next

Sorry for my bud english but i'm italian...

I'm new user, i use B4J from 5 days and seem very good...

How to create additional libraries for B4J? Need to use Java or possible using only Class in B4J IDE?
I've created some libraries for other languages, so i want translate to B4J

Can someone please explain how to do it?
 
Last edited:

giga

Well-Known Member
Licensed User
Longtime User
Yes, This is the first version of this library. I have been working with the java code to allow for any drive to be selected. Originally I set the methods for the main drive letters a PC would be using. But now external, flash ,floppy and CD drives need to be considered A-Z. I should have a new version out by next week.

THANKS for the feedback.
 

maxer73

Member
Great!

thanks for your library....

How to create additional libraries for B4J?
Need to use Java or possible using only Modules and Classes in B4J IDE?

I've created some libraries for other languages, so i want translate to B4J
 

giga

Well-Known Member
Licensed User
Longtime User
Great!

thanks for your library....

How to create additional libraries for B4J?
Need to use Java or possible using only Modules and Classes in B4J IDE?

I've created some libraries for other languages, so i want translate to B4J


Erel has a great tutorial I went from.
http://www.b4x.com/android/forum/threads/creating-libraries-for-basic4android.6810/

I use eclipse and library compiler. You use java in eclipse then compile with jlibrarycompiler.
**Note some libraries are easy and some are difficult to convert(if at all) to B4J.

Good Luck.
 
Top