How to use date format in a path to copy files

1.1k views Asked by At

I have written a code to copy files to "current date" folder. So, I want to use date in file path CopyData "E:\From\", "D:\To\ & Format(Date, 'dd-mm-yyyy')& '\'" How is it possible?.

My code is below:

Public Sub PerformCopy()
    MkDir "D:\To\" & Format(Date, "dd-mm-yyyy")
    CopyData "E:\From\", "D:\To\ & Format(Date, 'dd-mm-yyyy')& '\'"
End Sub

Public Sub CopyData(ByVal FromPath As String, ByVal ToPath As String)
Dim FSO As Object
Dim Fdate As Date
Dim FileInFromFolder As Object
Dim FolderInFromFolder As Object
    Set FSO = CreateObject("scripting.filesystemobject")
    'First loop through files
    For Each FileInFromFolder In FSO.getfolder(FromPath).Files
        Fdate = Int(FileInFromFolder.DateLastModified)

    If Fdate >= Date - 3 Then
        FileInFromFolder.Copy ToPath
    End If

    'Next loop throug folders
    For Each FolderInFromFolder In FSO.getfolder(FromPath).SubFolders
        CopyData FolderInFromFolder.Path, ToPath
    Next FolderInFromFolder
    Next
End Sub
1

There are 1 answers

0
YowE3K On BEST ANSWER

Your code

CopyData "E:\From\", "D:\To\ & Format(Date, 'dd-mm-yyyy')& '\'"

is passing two parameters to CopyData. The first parameter will be E:\From\ and the second parameter will be D:\To\ & Format(Date, 'dd-mm-yyyy')& '\'.

If you change that code to be

CopyData "E:\From\", "D:\To\" & Format(Date, "dd-mm-yyyy") & "\"

then the two parameters passed will be E:\From\ and (assuming today is 31 December 2016) D:\To\31-12-2016\, which is what I believe you want.