Copy files based on date modified/created.

LaBam

Registered User.
Local time
Today, 06:37
Joined
Mar 21, 2002
Messages
48
I use this code to copy all MS Word files from C:\Bam\Renewals to E:\Transfers\Updates. It works perfect.


Dim lb As Variant

Set lb = CreateObject("Scripting.FileSystemObject")
lb.CopyFile "C:\Bam\Renewals\*.doc", "E:\Transfers\Updates\"


What I want to do now is restrict it to copy only Word files modified/created the previous day. I tried incorporating the FileDateTime function but I don’t seem to have luck with it. Can anyone help with my problem?
 
You have to loop thru each file and check for the date.

Code:
Private Function GetYesterdyFiles()

    Dim objFS As Object, objFolder As Object
    Dim objFiles As Object, objF1 As Object
    Dim strFill As String, strFolderPath As String
        
    strFolderPath = "C:\Bam\Renewals\"
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder(strFolderPath)
    Set objFiles = objFolder.files
        
    For Each objF1 In objFiles
        If Right(objF1.Name, 3) = "doc" And objF1.DateCreated = DateAdd("d", -1, Date) Then
            objFS.CopyFile strFolderPath & objF1.Name, "E:\Transfers\Updates\"
        End If
    Next
        
    Set objF1 = Nothing
    Set objFiles = Nothing
    Set objFolder = Nothing
    Set objFS = Nothing
    
End Function
 
Tim K, thanks very much for the effort but I'm still having problems with it.

I wrote the code as you did. In fact, I just pasted it and called it from a click event. But it didn't copy any files and it gave no error message either. Indeed, there are files in these folders which were created a day before, always.

When I removed

And objF1.DateCreated = DateAdd("d", -1, Date)

from line 9, it copied all the .doc files.

I want to believe the problem is with the date formats. That is, the file date format is different from the system date format. The date format for the files is DDMMYYHHMM (eg. 12/11/02 15:30) whereas that of the system date is just DDMMYY (eg. 12/11/02)

I hope I'm not suggesting something stupid here.

More help needed, please.
 

Users who are viewing this thread

Back
Top Bottom