B4J Question DiskUtils: get free space of Linux path (drive)

peacemaker

Expert
Licensed User
Longtime User
Hi, All

This module works OK under Windows, getting the letters of the drives and getting the free space of them.
Under Linux this code works if the drive is set to "/".


B4X:
'bytes
Private Sub Get_FreeSpace(Fold As String) As Long
    '(ArrayList) [C:\, D:\, E:\, P:\, R:\]
    '(MyMap) {C:\=126100873216, D:\=125829115904, E:\=104857595904, P:\=258932207616, R:\=104857595904}
    '(MyMap) {C:\=30034104320, D:\=34107752448, E:\=20868218880, P:\=16199786496, R:\=20868218880}
    '(MyMap) {C:\=30034104320, D:\=34107752448, E:\=20868218880, P:\=16199786496, R:\=20868218880}
    '(MyMap) {C:\=96066768896, D:\=91721363456, E:\=83989377024, P:\=242732421120, R:\=83989377024}
  
    If DetectOS = "windows" Then
        Dim drive As String = Fold.SubString2(0, 3).ToUpperCase
    Else If DetectOS = "linux" Then
        Dim drive As String = "/"
    End If
  
  
    Private TotalDrives As List
    TotalDrives.Initialize
    TotalDrives=DiskUtils.GetDrives
    'Log (TotalDrives)
  
    Private TotalDriveCapacity As Map
    TotalDriveCapacity.Initialize
    TotalDriveCapacity=DiskUtils.GetDrivesCapacity
    'Log (TotalDriveCapacity)
  
    Private TotalUsableSpace As Map
    TotalUsableSpace.Initialize
    TotalUsableSpace=DiskUtils.GetUsableSpace
    'Log (TotalUsableSpace)
  
    Private TotalFreeSpace As Map
    TotalFreeSpace.Initialize
    TotalFreeSpace=DiskUtils.GetFreeSpace
    'Log(TotalFreeSpace)
  
    Private Writable As Map
    Writable.Initialize
    Writable=DiskUtils.Writable
  
    Private UsedSpace As Map
    UsedSpace.Initialize
    UsedSpace=DiskUtils.GetUsedSpace
    'Log(UsedSpace)
  

    Dim u As Long = TotalUsableSpace.Get(drive)
  
    Return u
  
    Dim t As Long = TotalDriveCapacity.Get(drive)
    Dim res As Int = (100-((t - u)/t) * 100)
    'Log (res)
    Return res
End Sub

Sub DetectOS As String
    Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
    If os.Contains("win") Then
        Return "windows"
    Else If os.Contains("mac") Then
        Return "mac"
    Else
        Return "linux"
    End If
End Sub

But as i can understand - it's not an universal way to get the info about any Linux path ? That may be located on any partition...
Correct ? Please, suggest to understand well.
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
Longtime User
Did you try GetSystemProperty("user.dir", "")?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Tried now, result it like "/home/userlogin/tempjars", where is the work folder of B4J bridge where jar uploaded and works.
But working with files can be by any other path, entered by user.
If these files are on another drive - how the path will be like (to check the free space)?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…