Android Code Snippet [B4X] CopyFolder / DeleteFolder

Discussion in 'Code Snippets' started by Erel, Aug 8, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    A recursive sub that copies a complete folder.

    Code:
    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:
    Code:
    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: Jan 10, 2019
  2. DonManfred

    DonManfred Expert Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice