Android Question Avoid altering LastModified Date when copying files

RB Smissaert

Well-Known Member
Licensed User
Longtime User
As I need to get rid of File.DirRootExternal (does not seem to work at all on Samsung S9) I need to copy a lot of files, mainly image files.
Now when copying these files the LastModified date is changed to the date time of the copy and I need to avoid that.

This is the altered code (original code from Erel) to achieve that:

Code: Copy
Private Sub CopyFolder(Source As String, targetFolder As String, bKeepModefiedDate As Boolean)
    
    Dim lFileDate As Long
    
    If File.Exists(targetFolder, "") = False Then File.MakeDir(targetFolder, "")
    
    If bKeepModefiedDate Then
        For Each f As String In File.ListFiles(Source)
            lFileDate = File.LastModified(Source, f)
            If File.IsDirectory(Source, f) Then
                CopyFolder(File.Combine(Source, f), File.Combine(targetFolder, f), True)
                Continue
            End If
            File.Copy(Source, f, targetFolder, f)
            AlterModifiedFileDate3(targetFolder, f, lFileDate)
        Next
    Else
        For Each f As String In File.ListFiles(Source)
            If File.IsDirectory(Source, f) Then
                CopyFolder(File.Combine(Source, f), File.Combine(targetFolder, f), False)
                Continue
            End If
            File.Copy(Source, f, targetFolder, f)
        Next
    End If
    
End Sub

Sub AlterModifiedFileDate3(strFolder As String, strFile As String, lDateTime As Long)
    
    Dim jo As JavaObject
    
    jo.InitializeNewInstance("java.io.File", Array(File.Combine(strFolder, strFile)))
    jo.RunMethod("setLastModified", Array(lDateTime))
    
End Sub

I thought somebody might find this useful.

RBS
 
Back
Top