Hi all,
from here I search to adapt:
https://www.b4x.com/android/forum/threads/b4x-copyfolder-deletefolder.69820/#content
I have this sub to recursively copy folders and files from ExternalStorage to DirInternal, but I cannot get it to work for subfolders. Folders and files are copied on first sub-folder but successive sub-folders are created to the root. Please, can someone help me to know how to fix it ?
I have the opposed sub that do the inverse, so copy recursively folders and files from DirInternal to ExternalStorage.
Currently I do not tested it. I need both subs in my application. Please, do you see something wrong here ?
Thanks in advance for any tip.
from here I search to adapt:
https://www.b4x.com/android/forum/threads/b4x-copyfolder-deletefolder.69820/#content
I have this sub to recursively copy folders and files from ExternalStorage to DirInternal, but I cannot get it to work for subfolders. Folders and files are copied on first sub-folder but successive sub-folders are created to the root. Please, can someone help me to know how to fix it ?
B4X:
Sub Class_Globals
Private lastPath As String
End Sub
Private Sub CopyFolderExternalToInternal (Source As ExternalFile, targetFolder As String) As Boolean
If Source.IsFolder = False Then Return False
If File.Exists(targetFolder, "") = False Then File.MakeDir(targetFolder, "")
For Each f As ExternalFile In Storage.ListFiles(Source)
Log("File name: " & f.Name)
If f.IsFolder Then
lastPath = f.Name
Log("Last folder: " & f.Name)
If File.Exists(File.DirInternal, lastPath) = False Then File.MakeDir(File.DirInternal, lastPath)
CopyFolderExternalToInternal(f, targetFolder)
Continue
End If
' External to Internal
Dim iStream As InputStream, oStream As OutputStream
Dim ef As ExternalFile = Storage.FindFile(Source, f.Name)
If ef.IsInitialized = True And ef.Length > 0 Then
Log("Save file: " & ef.Name & " to " & File.Combine(File.Combine(File.DirInternal,lastPath), ef.Name))
iStream = Storage.OpenInputstream(ef)
oStream = File.OpenOutput(File.Combine(File.DirInternal,lastPath), ef.Name, False)
File.copy2(iStream, oStream)
iStream.Close
oStream.Close
Else
Log("CopyFolderExternalToInternal: Cannot open file from external storage")
Return False
End If
Next
Return True
End Sub
I have the opposed sub that do the inverse, so copy recursively folders and files from DirInternal to ExternalStorage.
Currently I do not tested it. I need both subs in my application. Please, do you see something wrong here ?
B4X:
Private Sub CopyFolderInternalToExternal(Source As String, targetFolder As ExternalFile) As Boolean
If File.IsDirectory(Source, "") = False Then Return False
' Storage.FindDirOrCreate(targetFolder, "")
For Each fName As String In File.ListFiles(Source)
If File.IsDirectory(Source, fName) Then
CopyFolderInternalToExternal(File.Combine(Source, fName), Storage.FindDirOrCreate(targetFolder, fName))
Continue
End If
' Internal to External
Dim iStream As InputStream, oStream As OutputStream
Dim ef As ExternalFile = Storage.FindFileOrCreate(targetFolder, fName)
If ef.IsInitialized = True Then
iStream = File.OpenInput(File.DirAssets, fName)
oStream = Storage.OpenOutputStream(ef)
File.copy2(iStream, oStream)
iStream.Close
oStream.Close
Else
Log("CopyFolderInternalToExternal: Cannot open file from external storage")
Return False
End If
Next
Return True
End Sub
Thanks in advance for any tip.
Last edited: