B4J Code Snippet Copy And Delete Folders

EDIT: Consider to use FileUtils that contains this and other methods

I hope they can be useful.
B4X:
'Similar to File.Copy() but also folders with content can be copied
Sub Copy(DirSource As String, FileSource As String, DirTarget As String, FileTarget As String)
    If File.IsDirectory(DirSource, FileSource) Then
        Dim sourcePath As String = File.Combine(DirSource, FileSource)
        Dim targetPath As String = File.Combine(DirTarget, FileTarget)
        Dim sourceFiles As List = File.ListFiles(sourcePath)
        If sourceFiles.IsInitialized = False Then Return 'Return if folder is not accessible
        File.MakeDir(DirTarget, FileTarget)
        For Each name As String In sourceFiles
            If File.IsDirectory(sourcePath, name) Then
                Copy(sourcePath, name, targetPath, name)
            Else
                File.Copy(sourcePath, name, targetPath, name)
            End If
        Next
    Else
         File.Copy(DirSource, FileSource, DirTarget, FileTarget)
    End If
End Sub

B4X:
'Similar to File.Delete() but also folders with content can be deleted
Sub Delete(Folder As String, FileName As String)
    If File.IsDirectory(Folder, FileName) Then
        Dim completePath As String = File.Combine(Folder, FileName)
        Dim files As List = File.ListFiles(completePath)
        For Each name As String In files
            If File.IsDirectory(completePath, name) Then
                Delete(completePath, name)
            End If
            File.Delete(completePath, name)
        Next
    End If
    File.Delete(Folder, FileName)
End Sub
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…