User Tools

Site Tools


6.12.0 Temporary directories and temporary files

A temporary directory is used to store files for a limited time. You use temporary files to store data for a limited period of time. Temporary directories and temporary files are automatically deleted by the operating system after the Gambas programme ends.

For example, if you archive your scans in TIFF files to convert them later to PNG or JPG format, it is a good idea to copy a selection of TIFF files into a temporary directory and then convert them. Finally, exit your Gambas conversion programme, then the selected TIFF files in the temporary directory and also the temporary directory are automatically deleted.

Temporary files are needed if, for example, you want to use a Bash script in a Gambas programme and the script is located in the project folder in a data directory. If you call the script at runtime after starting the executable file *.gambas, you will end up with an error message because you do not have permission to execute the script. The reason is that all files in the Gambas archive *.gambas can only be read! A way out with the use of a temporary file that you can read and execute is briefly outlined below:

Public sScriptPath As String
Public sTempScriptPath As String
Public sStore As String
Public Sub Form_Open()
  sScriptPath = "./data/"               ' Set path to the script (original)
  sTempScriptPath = Temp(File.BaseName(sScriptPath))  ' Set path to a temporary file
  Copy sScriptPath To sTempScriptPath                 ' Copy original script to temporary file
  Chmod sTempScriptPath To "r-xr-xr-x"                ' Set execution rights for the copy
' Chmod sTempScriptPath To "..x..x..x"                ' Alternative
Public Sub btnRunScript_Click()
  Shell sTempScriptPath To sStore                     ' Script copy in a shell instruction' Execute with the required rights ...

The temporary file /tmp/gambas.1000/<PID>/test_script.tmp - which was worked with - is automatically deleted after the programme ends. Functions Temp() and Temp$()

File name = Temp$( [ Prefix ] )
File name = Temp( [ Prefix ] )

The Temp() and Temp$() functions return the path for a temporary file or directory. The path has the following form:

  • UserID is the system identifier of the user.
  • ProcessID is the process ID (PID) of the current process.
  • Prefix represents an (optional) base file name (without extension!).
  • The extension of a temporary file or directory is always .tmp and is assigned automatically.

If no prefix is given, it is replaced by an integer which is automatically incremented with each further call of this function. In this way, the returned path name is always unique. This function returns only one path. You can then use this path to temporarily create, for example, a file, a directory, a Unix socket or a symlink.

Important to know:

  • A logged-in system user has write and read rights to the temporary directory or file.
  • All files in the temporary directory /tmp/gambas.<UserID>/<ProcessID><*.tmp> are automatically removed after the Gambas programme ends.
  • The directory /tmp/gambas.<UserID>/<ProcessID><*.tmp> is also deleted.


  Dim sTempDir As String = Temp$()
  Mkdir sTempDir
' Unpacks an archive into the created temporary directory.
  Exec ["tar" "-zxf", "<path_to_archiv>/archive.tar.gz", "-C", sTempDir] Wait
' If the instruction EXEC [...] generates an error, you will be informed like this:
  If Process.LastValue Then Print Error.Code

With this source code section

' Gambas class file
Public Sub btnStart_Click()
  Dim sRuntimeDir, sTempDir As String
' 1. Call of the function Temp$()
  sTempDir = Temp$()
  txaResults.Insert("File-Path 1  = " & sTempDir & gb.Lf)
  File.Save(sTempDir, "1")
' 2. Call of the function Temp() - alternative syntax
  sTempDir = Temp()
  txaResults.Insert("File-Path 2  = " & sTempDir & gb.Lf) ' 2. Calling the function with alternative syntax
  File.Save(sTempDir, "2")
' 3. Call of the function Temp(...) with default of a freely defined file name 'test'
  sTempDir = Temp$("test")
  txaResults.Insert("File-Path 3  = " & sTempDir & gb.Lf)
  File.Save(sTempDir, "3")
' 4. Creation of the file with the path /tmp/gambas.1000/<PID>/link.tmp and the content ""
  sTempDir = Temp$("content")
  txaResults.Insert("File-Path 4  = " & sTempDir & gb.Lf)
  File.Save(sTempDir, "") ' Datei
  txaResults.Insert("File-Content = " & File.Load(sTempDir) & gb.Lf)
' 5. Creation of a file in a special temporary directory
  sRuntimeDir = Desktop.RuntimeDir &/ "time.txt"
  txaResults.Insert("File-Path 5  = " & sRuntimeDir & gb.Lf)
  File.Save(sRuntimeDir, Date.ToUnixTime(Now())) ' File
  txaResults.Insert("Unix Timestamp = " & File.Load(sRuntimeDir) & gb.Lf)
  txaResults.Insert("Date from Timestamp = " & Date.FromUnixTime(File.Load(sRuntimeDir)) & gb.Lf)
' 6. Creation of an empty text file in a temporary directory
  sTempDir = File.Dir(Temp()) &/ "gambas-book"
  If Exist(sTempDir) Then
     Rmdir sTempDir
' If Exist(sTempDir) Then Shell.RmDir(sTempDir) ' Effective alternative!
  File.Save(sTempDir &/ "values.log", "")
  txaResults.Insert("File-Path 6  = " & sTempDir &/ "values.log" & gb.Lf)
Public Sub DeleteDirRecursively(sDir As String)
  Dim sFile, sDirectory As String
  For Each sFile In RDir(sDir, "*")
    If Not IsDir(sFile) Then
     ' Print "File-Path: ", sDir &/ sFile
       Kill sDir &/ sFile
  For Each sDirectory In RDir(sDir, "*", gb.Directory)
  ' Print "Folder: ", sDirectory
    Rmdir sDirectory

results in these outputs:

Figure Results 1

Another launch results in new temporary files as well as changed contents:

Figure Results 2

The special feature in the source code is the deletion of the temporary directory with the alternative approaches via Shell.RmDir(sTempDir) and the procedure DeleteDirRecursively(sDir As String). Digression: Property Desktop.RuntimeDir (gb.desktop)

The /run directory is a virtual temporary file system ( It exists in the main memory and is automatically emptied when the computer is restarted. The environment variable $XDG_RUNTIME_DIR defines a good place for your runtime files. It is the directory /run/user/<user-id> if the environment variable $XDG_RUNTIME_DIR is set to the default value. It seems that /run/user/<user-id> rivals the /tmp directory. Obviously /run seems to be more modern, as indicated by the description on It's certainly a good plan if each user has their own (temporary) directory under /run/user/<user-id> from the start.


Although Gambas currently only supports /tmp with Temp$() and generates its “gambas.<user-id>” itself, you also have access to the (base) directory /run/user/1000. You get access as a user via the value of the property Desktop.RuntimeDir (gb.desktop). This property (data type string) returns the base directory in which user-specific, but not necessarily runtime-required data and other stream objects such as Unix sockets or named pipes are to be stored. The user must be the only one with Unix access mode 0700 who has full access to the /run/user directory - which can be quickly checked:

hans@mint-183 ~ $ cd /run/user
hans@mint-183 /run/user $ ls -l
in total 0
drwx------ 7 hans hans 180 Aug 24 10:37 1000

See chapter '6.1 Paths in Gambas' for further instructions.


The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k6/k6.12/start.txt · Last modified: 16.01.2022 (external edit)

Page Tools