Android Question File.Copy Question

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am copying a file from the DefaultExternal directory to the DirInternal directory.

I am checking and copying the file like:

B4X:
If File.Exists(rp.GetSafeDirDefaultExternal(""), "myFile.db") = True Then
        File.Copy(rp.GetSafeDirDefaultExternal(""),"MyFile.db",File.DirInternal,"myFile.db")   
    End If

I am running the above code when the app first opens. (from the Starter Service - Service_Create)

At a later time, I am deleting the file in DefaultExternal directory, so the above code won't run next time the app runs.

If the code at a later time doesn't run to delete the file then next time the user opens the app, the file will still exist and it will run the above code again.

The question I have is, if the file still exists in the defaultExternal and I was to copy the file again (as the code above) would it detect that the file already exists in the new location and not copy or would it still copy the file and replace it ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: Don't use rp.GetSafeDirDefaultExternal("") multiple times. Set this folder in Service_Create of the starter service and use Starter.SourceFolder from now on (or any other meaningful name).

The question I have is, if the file still exists in the defaultExternal and I was to copy the file again (as the code above) would it detect that the file already exists in the new location and not copy or would it still copy the file and replace it ?
It doesn't check whether the target file exists. It overwrites it if needed.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Tip: Don't use rp.GetSafeDirDefaultExternal("") multiple times. Set this folder in Service_Create of the starter service and use Starter.SourceFolder from now on (or any other meaningful name).
Is this the correct way?

Starter:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
        Dim rp As RuntimePermissions
        Dim SourceFolder As String
End Sub

Sub Service_Create
    SourceFolder = rp.GetSafeDirDefaultExternal("")
End Sub

Main (or another module):
B4X:
If File.Exists(Starter.SourceFolder, "MyFile.db") = True Then
        File.Copy(Starter.SourceFolder,"MyFile.db",File.DirInternal,"MyFile.db")
    End If
 
Upvote 0
Top