0

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
Community
  • 1
  • 1
Adnan
  • 167
  • 1
  • 5
  • 17
  • Can you tell more about folders structure? Following your example, in `E:\From\ ` you have every file in one directory, or maybe this folder has subfolders? In `D:\To\ ` you want to put everything in subfolder named by todays data, right? – Limak Dec 30 '16 at 14:28
  • The folder `E:\From\ ` has subfolders so I want to copy files from all subfolders to `D:\To\ ` folder. – Adnan Dec 30 '16 at 14:44
  • Please check if my approach from this post works for you http://stackoverflow.com/questions/41345805/run-excel-macro-recursive-on-all-directories-inside-of-a-folder-and-so-on/41346575#41346575 – Limak Dec 30 '16 at 14:50
  • To perform batch process like this there is VBScript – Shai Rado Dec 30 '16 at 16:58

1 Answers1

0

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.

YowE3K
  • 23,852
  • 7
  • 26
  • 40