B4J Code Snippet Disk Space Utils

**UPDATED to V1.1 NOW WITH EVENTS TO DETECT IF NEW DRIVES ARE ADDED OR REMOVED.

This is a code module, wich you can add to your projects and that I would like to share. It is very usefull to me, and I hoppe it can be of any use to others.

It uses the Java API to get the results.

There is a Code Module named DiskUtils. In this code Module there are some Subs that return lists or Maps

In your project you can call the following Subs

* DiskUtils.GetDrives
* DiskUtils.GetDrivesCapacity
* DiskUtils.GetUsableSpace
(*new in V1.1)
* DiskUtils.GetFreeSpace
* DiskUtils.CanWrite
* DiskUtils.GetUsedSpace
(*new in V1.1)


GetDrives will return a list with all available drives.

GetDrivesCapacity will return a map with all available disk drives and their size in bytes.

GetUsableSpace Will return a map with all available disk drives and their usable space for the JavaVM in bytes (usualy this values are the same as DrivesCapacity).

GetFreeSpace will return a map with all available disk drives and the available free space in bytes

CanWrite will return a map with all available disk drives and a value of True or False, depending if the drive is writable or not.

GetUsedSpace will return a map with all available disk drives and the used space in bytes.

NEW in version 1.1

Events

Detecting if a drive (USB pen for example) is plugged in or removed is now possible.

If you need to detect if new drives are added or removed, in your application code you should initialize the events with

* diskUtils.InitializeEvents (module As Object,frequency As Int)

module is the name of the module where you want your events to be handled (usualy it will be Me), frequency is the time in miliseconds that you want to scan your drives for changes.

InitializeEvents expects to find in you application code the following subs

Sub DisksAdded_Event

Sub DisksRemoved_Event

in each one of them you place the code you want to execute when disks are added or removed respectively.

If you want to disable the events you can execute

*DiskUtils.TerminateEvents

Example is self explanatory and simple to follow.

Ocasionaly I will update with new functions or improvements.

All the best

JM
 

Attachments

  • drives.png
    drives.png
    11.4 KB · Views: 662
  • DiskUtilsV1.1.zip
    2.3 KB · Views: 698
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Fixed the trouble and the v.1.2 module file is attached.

B4X:
Drives(i) 'was changed to...
Drives(i).As(String)
 

Attachments

  • DiskUtils.bas
    4.9 KB · Views: 299
Last edited:
Top