Solved Need to copy yesterday's file only

April 14, 2016 at 06:59:50
Specs: Windows 7
Hi,

I need a script to copy the files that is created yesterday, like if today is April 14, I only want the files from April 13. I'm running windows OS.

message edited by Raffa


See More: Need to copy yesterdays file only

Report •


#1
April 19, 2016 at 02:57:16
Here is a possible solution but you will need to test to see if the concept works, I have tested it and it seems to copy all of yesterdays (18/04/2016) to another folder....

What you will need to do is

Open Excel
Hold Alt and press F11 to open the VBA IDE
Double click on sheet1 from the top left project explorer
paste the code
then place the cursor over 'CopyYesterdaysFiles' and press F5 to run

Make sure you:

Set RootFolder to the path where you want to copy the files from
Set DestinationFolder to where the files should be copied to

What the code does is loop through all the files in the source folder, extract the modified date from the attributes of the file, check if the date is yesterdays date then copy the file to the destination folder, if the date is in fact yesterday. Not sure if we need to get the date modified or attribute or something else, test and see how this code works for you.

Sub CopyYesterdaysFiles()
        
    Dim FSO
    Dim Fol
    Dim RootFolder As String
    Dim DestinationFolder As String
    
    RootFolder = "C:\SourceFolder\"
    DestinationFolder = "C:\DestinationFolder\Copy"
     
    If Right(RootFolder, 1) <> "\" Then
        RootFolder = RootFolder & "\"
    End If
    
    If Right(DestinationFolder, 1) <> "\" Then
        DestinationFolder = DestinationFolder & "\"
    End If
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Fol = FSO.GetFolder(RootFolder)
    
    For Each Fil In Fol.Files
        
        If DateDiff("d", Format(Now(), "dd/mm/yyyy"), Left(Fil.datelastmodified, 10)) = -1 Then
            FileCopy RootFolder & Fil.Name, DestinationFolder & Fil.Name
        End If

    Next Fil
    
End Sub

message edited by AlwaysWillingToLearn


Report •

#2
April 19, 2016 at 03:57:03
✔ Best Answer
Here is also a VBS solution, just a modification of the above

    Dim FSO
    Dim Fol
    Dim Fil
    Dim RootFolder
    Dim DestinationFolder
    
    RootFolder = "C:\SourceFolder\"
    DestinationFolder = "C:\DestinationFolder\"
   

    If Right(RootFolder, 1) <> "\" Then
        RootFolder = RootFolder & "\"
    End If
    
    If Right(DestinationFolder, 1) <> "\" Then
        DestinationFolder = DestinationFolder & "\"
    End If
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Fol = FSO.GetFolder(RootFolder)
    
    For Each fil In Fol.Files

        If DateDiff("d",Left(Now(), 10), Left(fil.datelastmodified, 10)) = -1 Then
            FSO.CopyFile RootFolder & fil.Name, DestinationFolder & fil.Name
        End If

    Next
    


Report •
Related Solutions


Ask Question