[Solved] How to Determine Free Space on a USB Flash Drive

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Steve R.
Posts: 163
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

[Solved] How to Determine Free Space on a USB Flash Drive

Post by Steve R. »

I have a Base program that collects a list of MP3 filenames that will be copied to a USB drive. I can compute the total space that will be taken for the file copy operation. However, I have been unsuccessful in figuring out a Base macro that would compute the free space available on the USB flash drive.

I first attempted to use the shell function, but that did not work. I tried various permutations (including using pwd) but kept getting 0. (df by itself lists the free space on all mounted devices.)

Code: Select all

strSize=shell("bash df")
I have also located getFreeSpace under the API documentation.
Unfortunately, I do not know enough on how to utilize this method, assuming that it would be possible to use it in a Base macro.

Any suggestions on how to fix or utilize an alternative approach?
Last edited by Steve R. on Tue Mar 11, 2014 2:35 am, edited 1 time in total.
Ubuntu 16.04 and Windows 10
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: How to Determine Free Space on a USB Flash Drive

Post by B Marcelly »

Hi,
Several problems here.
  • You are writing Base, but you probably mean Basic programming language.
  • I don't think the Shell function can return you such information. If it returns something, it is an execution indicator (0 = OK, 1 = error).
    The API execute() from XSystemShellExecute returns nothing.
  • The documentation you reference is for designers of OpenOffice itself, working on sources with C++.
    For applications, browse the API IDL. And you will find nothing.
  • There is no Basic instruction about drive (or volume).
This information is system-dependent.

In MS-Windows you can get system information with the File System Object (FSO), that is available through COM-Automation.

Code: Select all

Sub Main
MsgBox(getDriveFreeSpace("D") & " Mbytes")
End Sub


Function getDriveFreeSpace(driveName As String) As Long
Dim fso as object, drv as object

fso = CreateObject("Scripting.FileSystemObject")
if fso.DriveExists(driveName)  then
  drv = fso.GetDrive(driveName)
  getDriveFreeSpace = drv.FreeSpace / (1024*1024)
else
  getDriveFreeSpace = 0
end if
End Function
Note that the free space value may be greater than the capacity of a Long variable in Basic (32 bits). However the code computes correctly the Megabytes value (probably because drv.FreeSpace returns a Decimal type).

For Unix-like or Mac, I don't know a solution.

A general work-around could be to fill the drive until the output throws an error.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to Determine Free Space on a USB Flash Drive

Post by Villeroy »

http://atlee.ca/posts/blog20080223getti ... ython.html

<user_profile>/Script/python/FileSystem.py

Code: Select all

import os

def freespace(p):
    """ Returns the number of free bytes on the drive that p is on """ 
    s = os.statvfs(p)
    return s.f_bsize * s.f_bavail 

Code: Select all

REM  *****  BASIC  *****
REM Keep a global reference to the ScriptProvider, since this stuff may be called many times:
Global g_MasterScriptProvider
REM Specify location of Python script, providing cell functions:
Const URL_Main = "vnd.sun.star.script:FileSystem.py$"
Const URL_Args = "?language=Python&location=user"
Function getFreeSpace(s)
   sURL = URL_Main & "freespace" & URL_Args
   oMSP = getMasterScriptProvider()
   oScript = oMSP.getScript(sURL)
   x = oScript.invoke(Array(s),Array(),Array())
   getFreeSpace = x
End Function

Function getMasterScriptProvider()
   if NOT isObject(g_MasterScriptProvider) then
      oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
      g_MasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
   endif
   getMasterScriptProvider = g_MasterScriptProvider
End Function
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Steve R.
Posts: 163
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: How to Determine Free Space on a USB Flash Drive

Post by Steve R. »

B Marcelly wrote:Hi,
Several problems here.
  • You are writing Base, but you probably mean Basic programming language.
  • I don't think the Shell function can return you such information. If it returns something, it is an execution indicator (0 = OK, 1 = error).
    The API execute() from XSystemShellExecute returns nothing.
  • The documentation you reference is for designers of OpenOffice itself, working on sources with C++.
    For applications, browse the API IDL. And you will find nothing.
  • There is no Basic instruction about drive (or volume).
This information is system-dependent.

In MS-Windows you can get system information with the File System Object (FSO), that is available through COM-Automation.

Code: Select all

Sub Main
MsgBox(getDriveFreeSpace("D") & " Mbytes")
End Sub

Function getDriveFreeSpace(driveName As String) As Long
Dim fso as object, drv as object

fso = CreateObject("Scripting.FileSystemObject")
if fso.DriveExists(driveName)  then
  drv = fso.GetDrive(driveName)
  getDriveFreeSpace = drv.FreeSpace / (1024*1024)
else
  getDriveFreeSpace = 0
end if
End Function
Note that the free space value may be greater than the capacity of a Long variable in Basic (32 bits). However the code computes correctly the Megabytes value (probably because drv.FreeSpace returns a Decimal type).

For Unix-like or Mac, I don't know a solution.

A general work-around could be to fill the drive until the output throws an error.
Thanks. The program that I am working on in Base, was originally an MS Access program and I used the FileSystemObject code you posted above. I was hoping that Base would have a similar capability, but I have not run across that yet. My "solution" was to use a BASH script and import the data into base. See below.
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
Posts: 163
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: How to Determine Free Space on a USB Flash Drive

Post by Steve R. »

Villeroy wrote:http://atlee.ca/posts/blog20080223getti ... ython.html

<user_profile>/Script/python/FileSystem.py

Code: Select all

import os

def freespace(p):
    """ Returns the number of free bytes on the drive that p is on """ 
    s = os.statvfs(p)
    return s.f_bsize * s.f_bavail 

Code: Select all

REM  *****  BASIC  *****
REM Keep a global reference to the ScriptProvider, since this stuff may be called many times:
Global g_MasterScriptProvider
REM Specify location of Python script, providing cell functions:
Const URL_Main = "vnd.sun.star.script:FileSystem.py$"
Const URL_Args = "?language=Python&location=user"
Function getFreeSpace(s)
   sURL = URL_Main & "freespace" & URL_Args
   oMSP = getMasterScriptProvider()
   oScript = oMSP.getScript(sURL)
   x = oScript.invoke(Array(s),Array(),Array())
   getFreeSpace = x
End Function

Function getMasterScriptProvider()
   if NOT isObject(g_MasterScriptProvider) then
      oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
      g_MasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
   endif
   getMasterScriptProvider = g_MasterScriptProvider
End Function
Thanks for the Python script, currently beyond my capabilities. Moving from MS Access to Base is proving to be challenging. I assume from the quote below that the Python script is external to base and that it is "imported" into Base? In taking a quick internet search, it appears that Python will be a programming language that I will need to learn.
REM Specify location of Python script, providing cell functions:
Does the line below inform Base that a Python script is being used?
Const URL_Main = "vnd.sun.star.script:FileSystem.py$"
Const URL_Args = "?language=Python&location=user"
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
Posts: 163
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: [Solved] How to Determine Free Space on a USB Flash Driv

Post by Steve R. »

The solution, which I don't particularly care for is to use a Bash script to get the available space on the USB drive, dump the answer in a text file, then import that text file into Base. It's messy, but works.

I was hoping that the structure shown below would have worked with Base, alas it did not. Maybe someone does know how to make it work.
The code below is on Page 503 of "Useful Macro Information For OpenOffice.org" By Andrew Pitonyak

Code: Select all

vRC = Shell("C:\andy\TSEProWin\g32.exe", 2, "c:\Macro.txt")
The line of code below executes the bash script. The variable "strDisplayDestination" is the location of the USB drive as captured by the FolderPicker

Code: Select all

shell("/home/steve/MP3_Copy_Project/USBFreeSpace.sh " + strDisplayDestination)
The Bash script below writes the available space found on the USB drive to the text file "USBFreeSpace.txt".

Code: Select all

#!/bin/bash 
df ${1} | tr -s ' ' ' ' | cut -f4 -d' ' | sed "1 d" > /home/steve/MP3_Copy_Project/USBFreeSpace.txt
The (expanded) code below, located in Base, copies the available space into a Base (HSQLDB) table so that it is available to the program.

Code: Select all

If TableExists(strUSBTableName) Then			
    Dim oForm As Object
    Dim dblUSBSize as double
    Call Application.OpenConnection(ThisDatabaseDocument)
    Set oForm = AllForms(strFormName)  
    docmd.runSQL("SET TABLE """ + strUSBTableName + """ SOURCE OFF;")				
    shell("/home/steve/MP3_Copy_Project/USBFreeSpace.sh " + strDisplayDestination) 
    docmd.runSQL("SET TABLE """ + strUSBTableName + """ SOURCE ""USBFreeSpace.txt"";")
    docmd.runCommand("DBRefreshTables")
    Set oRecordset = Application.CurrentDb().OpenRecordset("USBTableName") 
    oRecordset.movefirst           
    dblUSBSize= Round(CDbl(oRecordset.Fields("FREESPACETXT").Value) *GigabyteConv,4)            
    Forms(strFormName).Controls("TextBox6").value =  str(dblUSBSize)                
    oRecordset.mclose 
end if
Ubuntu 16.04 and Windows 10
Post Reply