everblazing
Board Regular
- Joined
- Sep 18, 2015
- Messages
- 156
Hi
I am trying to build a macro that will move last month's files to a different location.
files names are BDP_171002.CSV to BDP_171031.CSV. These files are generated based on weekdays.
I want to move files based on last month file names NOT date modified. what do i need to adjust on my macro below.
Appreciate any help.
I am trying to build a macro that will move last month's files to a different location.
files names are BDP_171002.CSV to BDP_171031.CSV. These files are generated based on weekdays.
I want to move files based on last month file names NOT date modified. what do i need to adjust on my macro below.
Appreciate any help.
Code:
Sub Copy_Files_Dates()'This example copy all files between certain dates from FromPath to ToPath.
'You can also use this to copy the files from the last ? days
'If Fdate >= Date - 30 Then
'Note: If the files in ToPath already exist it will overwrite
'existing files in this folder
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim Fdate As Date
Dim FileInFromFolder As Object
FromPath = "U:\EXCEL\Report" '<< Change
ToPath = "U:\EXCEL\Report\toPathfolder" '<< Change
Lastdayofmonth = DateSerial(Year(Date), Month(Date), 0)
firstdayofLastmonth = DateSerial(Year(Date), Month(Date) - 1, 1)
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
If Right(ToPath, 1) <> "\" Then
ToPath = ToPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If FSO.FolderExists(ToPath) = False Then
MsgBox ToPath & " doesn't exist"
Exit Sub
End If
For Each FileInFromFolder In FSO.getfolder(FromPath).Files
Fdate = Int(FileInFromFolder.DateLastModified)
'Copy files from 1-Oct-2017 to 31-Oct-2017
If Fdate >= DateSerial(2017, 10, 1) And Fdate <= DateSerial(2017, 10, 31) Then
FileInFromFolder.Copy ToPath
End If
Next FileInFromFolder
MsgBox "You can find the files from " & FromPath & " in " & ToPath
End Sub