One-Time Copy Files from Assets to Defaultexternal

Bill Norris

Active Member
Licensed User
As I have developed this app, I have ended up with a lot of files in dir.assets that I want to move to dir.defaultexternal. What would be the easiest code to do this -- one time shot? Basically, copy every file from dir.assets into dir.defaultexternal, then I can go into dir.defaultexternal using file manager on the device and delete those files that actually should stay in dir.assets -- bal files, etc., and I would go into the project Files folder and delete those files that now reside in dir.defaultexternal.
 

Mahares

Expert
Licensed User
How about this code snippet Bill. I tried it. It worked for me, unless one of the forum gurus comes up with a better solution:
B4X:
Dim MyList As List
Dim MyFile As String
Dim i As Short
Dim FilePathSource As String        :FilePathSource=File.DirAssets
Dim FilePathDest As String        :FilePathDest=File.DirDefaultExternal


    MyList.Initialize                   
    MyList=File.ListFiles(FilePathSource)
    For i=0 To MyList.Size-1
        MyFile=MyList.Get(i)
    If MyFile.EndsWith(".bal")=False  Then      'do not copy the .bal files
       Try 
'      Msgbox(MyFile,"")
        File.Copy(FilePathSource,MyFile,FilePathDest,MyFile)
      Catch
      End Try
      End If
    Next
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
This code is from DBUtils:
B4X:
Sub CopyDBFromAssets (FileName As String) As String
   Dim TargetDir As String
   If File.ExternalWritable Then TargetDir = File.DirDefaultExternal Else TargetDir = File.DirInternal
   If File.Exists(TargetDir, FileName) = False Then
         File.Copy(File.DirAssets, FileName, TargetDir, FileName)
   End If
   Return TargetDir
End Sub
As you can see it checks whether there is a writable storage card and if there isn't the file is copied to the internal folder. It returns the target folder which you should use to access the files.
 
Upvote 0

Mahares

Expert
Licensed User
@Erel: How do you cycle through all the File.DirAssets in you sample code. He is interested in copying more than one file in DirAssets to DirDefault External. In my code, I had to trap for file names that showed up like databases, images, webkit for it to work. That is why I had the try, catch, end try.
 
Upvote 0

Mahares

Expert
Licensed User
@Bill Norris: Could you please post your code that cycles through all the files, since you mentioned you had a solution comparable to mine. In this case, I could only get mine to run when I introduced the try, catch, end try keywords. I would like to see how you were able to copy multiple files from DirAssets without the error trapping.
 
Upvote 0

Bill Norris

Active Member
Licensed User
RE: Code To Copy

As requested:


B4X:
Sub Button2_Click
'this code will copy all files from dirassets to dirdefaultexternal
    Dim filelist As List
   filelist=File.ListFiles(File.DirAssets)
   Dim filename As String
   Dim i As Int
   For i=0 To filelist.Size-1
      filename=filelist.Get(i)
      File.Copy(File.DirAssets,filename,File.DirDefaultExternal,filename)
   Next
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
@ Bill Norris: Unless I trap your code with try - end try block, it returns the following error: Java file not found exception: databases. There are 3 other files in assets with the following names: images, sounds, Webkit that would return the same error. I do know know what these files are and where they came from. Perhaps, someone has encountered them and can tell me what those file names are and if I can get rid of them.
 
Upvote 0

Bill Norris

Active Member
Licensed User
Re:

The only files I have in assets are .bal files and some assorted graphics - .bmp, .jpg and .png. Not sure about those mystery files you mentioned. Perhaps make a backup copy of your project file and delete those mystery files and see if project runs ok??
 
Upvote 0

Mahares

Expert
Licensed User
You cannot delete files located in the Assets folder. Hopefully, one of the forum gurus has a solution to get rid of those stary files. The device is a Razr running 2.3.6 OS.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Run a log on filelist and see what is in it. It looks like you have subdirectories in your dirassests which won't copy over with Bill's code.

Sent from my DROIDX using Tapatalk 2
 
Upvote 0

Mahares

Expert
Licensed User
@Jon: If you read my post a couple of posts up, I list the 4 stray files in Assets folder:database, images, sounds, Webkit. The other files are .bal legit. files. I moved part of the code to a new project and still get the the same error, unless of course I trap it as shown in my code.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
As Bill suggested try removing those files. Can you see them in the Files tab?

Sent from my DROIDX using Tapatalk 2
 
Upvote 0

Mahares

Expert
Licensed User
Sorry JonPM: The stray files are not in the files tab. I would not have wasted any time going back and forth if they were in the tab. The only files I have are the .bal files which are legitimate. Hope things are well In Ca.
 
Upvote 0

Mahares

Expert
Licensed User
Originally the question was asked By Bill. I posted some code to help out. However, the code will not work unless I had to use a try-end try block. See my post at the near the top of the thread. He was able to use my code and a code similar to mine that he devised without the error trapping block. I was only trying the code on one of my apps to verify the validity of the code. The app I tried on has the .bal files on the files tab which copied OK ( from Dir.Assets to DirDefaultExternal). But, also the copy procedure tries to copy 4 files that I have no idea where they come from or even what they mean. They are not on the files tab. The 4 files that caused the app to crash are: database, images, sounds, Webkit. I have no idea how they got on the assets folder or what purpose they serve. They are not visible on the file tab on the right pane of the IDE. I hope it is clearer now.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
I see now. I've been reading using tapatalk which sometimes makes it harder to follow. Anyways, it seems that your issue is a local one, as I and many others around here have copied files using something similar to what Bill posted. So it seems weird to me (and Erel) to use a try catch block.

Sent from my DROIDX using Tapatalk 2
 
Upvote 0

Mahares

Expert
Licensed User
@Erel:
I performed the Tools, Clean files folder. But it gave me the following message: 'Unnecessary files were not found'
I also performed: Tools, Clean project and it did not help. I made a project search for one of the 4 rogue files: database, images, sounds, Webkit and found none. The app only runs when I introduce the Try-End Try block. I even moved that part of the code to another app and the result was the same on the Razr. I also ran the app on a 7 inch tablet and the result is the same. They seem harmless, but annoying. Would you recommend creating a new app, copy and paste the code and import the .bal files to the new one and trying it that way.
 
Upvote 0
Top