B4J Question How to copy the whole folder ( file.copyDirectory)?

cambopad

Active Member
Licensed User
Longtime User
Hi all friends!

How can I copy the whole folder to another location with B4J?

I cannot find the function file.copyDirectory in B4J . Are there any solutions?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Untested, beware:
B4X:
Sub CopyDirectory(source As String, dest As String)
    Dim al As List = File.ListFiles(source)
    For Each s As String In al
        If File.IsDirectory(source, s) Then
            CopyDirectory(File.Combine(source, s), File.Combine(dest, s))
        Else   
            File.Copy(source, s, dest, s)
        End If
    Next
End Sub
 
Upvote 0

sanjibnanda

Active Member
Licensed User
Longtime User
1. B4J: download the lib : db2000.B4J.eXtrafunctions.zip
2. extract the *.jar and *.xml into your additional lib folder
3. in your project, choose the lib
4. CopyDirectory("d:/path/of/folder/to/copy", "d:/path/of/target/folder/")
but it gives error

B4X:
B4J version: 4.00
Parsing code.    Error
Error parsing program.
Error description: Undeclared variable 'copydirectory' is used before it was assigned any value.
Occurred on line: 120 (Main)
CopyDirectory(File.DirApp&"\4", File.DirData("xTest"))

the code as given by Roycefer works.
Actually i want to copy a folder inside assets to Data directory. How is this possible?

thanks
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
The snippet in my earlier post should work with the File.DirAssets directory as the source. Of course, it won't work with that as the destination as you can't write to File.DirAssets.

By "Data directory", do you mean the directory returned by File.DirData("appname")? Just pass that as the dest parameter.
 
Upvote 0

sanjibnanda

Active Member
Licensed User
Longtime User
The snippet in my earlier post should work with the File.DirAssets directory as the source. Of course, it won't work with that as the destination as you can't write to File.DirAssets.

By "Data directory", do you mean the directory returned by File.DirData("appname")? Just pass that as the dest parameter.

CopyDirectory(File.DirAssets, File.DirData("xyz"))

it gives error as

java.lang.RuntimeException: Cannot list assets files
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Oh yes, my mistake. File.ListFiles() won't work in File.DirAssets. But you should already know what's in File.DirAssets since you put it there.
 
Upvote 0
Top