Copying Last month's files to a different location

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.

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
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello,

a proposal, which MUST be debugged!
Code:
sub MoveFiles()
oPath = "u:\excel\report\"
nPath = "u:\excel\report\toPath\"

iFile = dir(oPath & "*.xls?")
iMonth = Month(now) - 1'check, maybe worsheetfunction.month
do while len(iFile)
if mid(ifile,7,2) = iMonth then
    CreateObject("wscript.shell").exec("cmd /c Move "oPath & iFile " & nPath "") 
end if
iFile = dir
loop
end sub

To my understanding in vba there is no "move", but copy and kill. In DOS (cmd.exe) there MOVE exists.

regards
 
Upvote 0
Hello,

the following code was tested:

Code:
Sub iFen()
oPath = "c:\temp\"
nPath = "c:\temp\Daten\"
iFile = "test1.txt"
CreateObject("wscript.shell").exec ("cmd /c Move " & oPath & iFile & " " & nPath & "")
End Sub

regards
 
Upvote 0
Hello,

the following code was tested:

Code:
Sub iFen()
oPath = "c:\temp\"
nPath = "c:\temp\Daten\"
iFile = "test1.txt"
CreateObject("wscript.shell").exec ("cmd /c Move " & oPath & iFile & " " & nPath & "")
End Sub

regards

Thank you very much for replying and your effort in testing. how would i include this in my code? which part would that sit under?

Regards
Lucas
 
Upvote 0
Hello,

please test the code carefully (I didn't)

Code:
sub MoveLastMonthFiles()
 FromPath = "U:\EXCEL\Report\"  '<< Change
    ToPath = "U:\EXCEL\Report\toPathfolder\"    '<< Change
iFile = dir(FromPath & *.*)
do while len(iFile)
    if Month(FileDateTime(FromPath & iFile)) = Month(now)-1 then
        CreateObject("wscript.shell").exec ("cmd /c Move " & FromPath & iFile & " " & ToPath & "")
    end if
iFile = dir
loop
end sub

regards
 
Upvote 0
Hello, thank you for your reply.

I have tested the code and its not moving any files. just opens a command window (Black window) and close.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top