Android Code Snippet [B4X] CopyFolder / DeleteFolder

Erel

Administrator
Staff member
Licensed User
A recursive sub that copies a complete folder.

B4X:
Private Sub CopyFolder(Source As String, targetFolder As String)
   If File.Exists(targetFolder, "") = False Then File.MakeDir(targetFolder, "")
   For Each f As String In File.ListFiles(Source)
     If File.IsDirectory(Source, f) Then
       CopyFolder(File.Combine(Source, f), File.Combine(targetFolder, f))
       Continue
     End If
     File.Copy(Source, f, targetFolder, f)
   Next
End Sub
Note that it will not work with the assets folder. You can instead create a zip file and unpack it with the Archiver library.

Recursively delete a folder and its sub folders:
B4X:
Sub DeleteFolder (folder As String)
   For Each f As String In File.ListFiles(folder)
       If File.IsDirectory(folder, f) Then
           DeleteFolder(File.Combine(folder, f))
       End If
       File.Delete(folder, f)
   Next
End Sub

Tags: Copy, Folder, Directory
 
Last edited:
Top