Copy Files according to Time Range

lucour

Registered User.
Local time
Today, 20:08
Joined
Mar 7, 2001
Messages
60
I need some code that will copy all .txt files in directory C:\Bat to C:\Bat\Bat2. I only want to copy the files that were modified b/w 06:00 PM the day before and now. So far, this is what I have for code:

Public 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:\Bat\"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.GetFolder(strFolderPath)
Set objFiles = objFolder.files


For Each objF1 In objFiles
If Right(objF1.Name, 3) = "txt" And objF1.DateLastModified = DateAdd("d", -1, Date) Then
objFS.CopyFile strFolderPath & objF1.Name, "C:\Bat\Bat2\"
End If
Next

Set objF1 = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objFS = Nothing

End Function

Any help would be great............. Thanks!
 
Try this out.

Code:
...
If Right(objF1.Name, 3) = "txt" And (objF1.DateLastModified Between DateAdd("d", -1, Date) & " 06:00:00 PM" And Now) Then 
...
 
Thanks Tim. I tried this, but it keeps giving me a syntax error. "Expected: ) ".
 
Try this:

If Right(objF1.Name, 3) = "txt" And (objF1.DateLastModified >= DateAdd("d", -1, Date) + CDate("06:00:00 PM")) Then
 
Thanks a lot, Jon. Your solution worked perfectly.

Take care.
 

Users who are viewing this thread

Back
Top Bottom