B4X Store
jdashboardUI / Files & Compression / FileUtils
Files & Compression

FileUtils

Static utilities for recursive copy/delete, folders, renaming and file timestamps

Search all jdashboardUI docs
Source
jdashboardUI.b4xlib · Public API only
Store language: en

Files & Compression · jdashboardUI · B4J · StaticCode

FileUtils is a static file-system helper module. It complements ZipFile: use FileUtils to prepare, copy, clean or inspect folders and ZipFile when the final result must be an archive.

What it provides

  • Create missing parent directories for a file path.
  • Copy a single file or recursively copy a complete folder tree.
  • Run recursive copy through a ResumableSub.
  • Delete files or complete directory trees recursively.
  • Rename a file.
  • Return all descendant subfolders of a root folder.
  • Read and modify creation timestamps.
  • Modify the last-modified timestamp.

Public API

Method Purpose
createNonExistingDir(path) Creates missing parent directories
Copy(...) Copies a file or complete folder recursively
CopyAsync(...) ResumableSub version of recursive copy
Delete(...) Deletes a file or folder tree recursively
RenameTo(...) Renames a file
GetSubFolders(...) Returns all descendant folders
SetModifiedTime(...) Changes last-modified time
GetFileCreation(...) Reads creation time
SetFileCreation(...) Changes creation time

Create missing parent directories

createNonExistingDir expects a path that normally includes the final file name. Internally it calls File.GetFileParent(path) and creates any missing parent folders.

Dim TargetFile As String = File.Combine(File.DirApp, "exports/2026/october/report.pdf")
FileUtils.createNonExistingDir(TargetFile)

If you already have the directory itself rather than a future file path, File.MakeDir can be a better fit.

Recursive copy

Unlike the standard File.Copy, FileUtils.Copy also handles directories.

FileUtils.Copy( _
    File.DirApp, "data", _
    File.Combine(File.DirApp, "backup"), "data")

When the source item is a directory, the module creates the destination directory and recursively copies its children.

Asynchronous recursive copy

CopyAsync returns a ResumableSub. Use Wait For to receive the final Boolean result.

Private Sub CopyProject As ResumableSub
    Dim BackupRoot As String = File.Combine(File.DirApp, "backup")

    Wait For (FileUtils.CopyAsync( _
        File.DirApp, "data", _
        BackupRoot, "data")) Complete (Success As Boolean)

    If Success Then
        Log("Copy completed")
    Else
        Log("Copy failed")
    End If

    Return Success
End Sub

For critical backup operations, also verify the expected destination after completion.

Recursive delete

FileUtils.Delete(File.DirApp, "temporary-backup")

The method descends into a folder and removes its contents before removing the folder itself. This is destructive, so validate the path before calling it.

Rename a file

FileUtils.RenameTo(File.DirApp, "backup.tmp", "backup.zip")

The current implementation wraps java.io.File.renameTo. It does not expose the Java Boolean result, so code that requires confirmation should verify that the destination exists afterward.

Enumerate all subfolders

GetSubFolders recursively scans the root and returns full paths for every descendant folder. The root itself is not included.

Dim Folders As List = FileUtils.GetSubFolders(File.Combine(File.DirApp, "projects"))
For Each FolderPath As String In Folders
    Log(FolderPath)
Next

File timestamps

FileUtils.SetModifiedTime(File.DirApp, "report.pdf", DateTime.Now)

Dim Created As Long = FileUtils.GetFileCreation(File.DirApp, "report.pdf")
Log(DateTime.Date(Created) & " " & DateTime.Time(Created))

FileUtils.SetFileCreation(File.DirApp, "report.pdf", DateTime.Now)

Creation-time operations use Java NIO file attributes. Support ultimately depends on the operating system and file system.

A useful pattern is to keep temporary backup material outside the folder being archived. This avoids accidentally putting intermediate ZIP files inside another ZIP.

Private Sub CreateApplicationBackup As ResumableSub
    Dim StageParent As String = File.Combine(File.DirApp, "work")
    Dim StageName As String = "backup-stage"

    If File.Exists(StageParent, StageName) Then
        FileUtils.Delete(StageParent, StageName)
    End If

    File.MakeDir(StageParent, StageName)
    Dim StageDir As String = File.Combine(StageParent, StageName)

    Wait For (FileUtils.CopyAsync( _
        File.DirApp, "data", _
        StageDir, "data")) Complete (Copied As Boolean)

    If Copied = False Then Return False

    Dim Zip As ZipFile
    Zip.Initialize(File.Combine(File.DirApp, "backup.zip"))
    Zip.SmartCompression = True

    If Zip.AddFolderSmart(StageDir) = False Then
        Log(Zip.LastError)
        FileUtils.Delete(StageParent, StageName)
        Return False
    End If

    If Zip.ValidateArchive = False Then
        Log("Invalid ZIP: " & Zip.LastError)
        FileUtils.Delete(StageParent, StageName)
        Return False
    End If

    FileUtils.Delete(StageParent, StageName)
    Return True
End Sub

This separates responsibilities cleanly: FileUtils prepares and cleans working folders, while ZipFile creates and validates the archive.

  • FileChooserUtils — JavaFX FileChooser extension filters and default selection.
  • ZipFile + ZipFileHeader — ZIP creation, encryption, reading, modification, validation and inspection.

Best practices

  • Use Wait For with CopyAsync.
  • Double-check paths before recursive deletion.
  • Keep staging folders outside the data being compressed.
  • Verify the destination after RenameTo when the result matters.
  • Treat creation-time support as file-system dependent.