Macro to copy files

D

Dave Shaw

I have a number of directories containing mainly word documents (but some xls
and INI files) that I need to replicate on other network locations. I have
created a
macro that copies the relevant folders to the relevant servers. However as
there are circa 2000 documents that are replicated on 8 different servers it
takes a very long time to update.

What I wanted to know was whether I could change the macro to only copy
files modified between certain dates.

The code I am using is based upon the Sub Copy_Folder()
macro found at http://www.rondebruin.nl/folder.htm. ; This is copied several
times to copy different directories to different loactions (as I am rubbish
at loops).

Any help would be appreciated.

Thanks

Dave
 
R

Ron de Bruin

Hi Dave

Yes this is possible
I try to add a example this evening to the site
 
R

Ron de Bruin

Test this one for me Dave
Change thye dates

If Fdate >= "11/1/2006" And Fdate <= "11/7/2006" Then
Or use this for the files from the last 30 days
If Fdate >= Date - 30 Then



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 30 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 String
Dim FileInFromFolder As Object

FromPath = "C:\Data" '<< Change
ToPath = "C:\Test" '<< Change

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)
If Fdate >= "11/1/2006" And Fdate <= "11/7/2006" Then
FileInFromFolder.Copy ToPath
End If
Next FileInFromFolder

MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub
 
D

Dave Shaw

I seem to have a problem with this but can't work out why...

The macro seems to work fine until it reaches the third document in the
directory. For some reason it then tries to copy this document even though
the criteria is not met. Stepping into the macro shows that the Fdate is
"22/09/2006" and the criteria is >= Date - 5.

This also seems to happen for a few other files but not sure why.
 
R

Ron de Bruin

Try to test it tomorrow on a lot of files on my system Dave.
Maybe others see the problem in my code example ?

Bed time for me now
 
D

Dave Shaw

I wonder if the problem is related to date formats - I am in the UK so all my
dates are dd/mm/yyyy. The code appears to copy files (when the criteria is
not met) if the above date format would not be consistent with the US
mm/dd/yyy.

If that does not make sense here are a few examples, the criteria is to copy
if date is after 8/11/06 (8 Nov 2006)

7/11/06 - does not copy - i.e. the criteria works
23/10/06 - would copy i.e. the criteria does not work
12/10/06 does not copy - i.e. the criteria works

i.e. if the day is 13 or over.
 
D

Dave Peterson

With no testing at all...

this
If Fdate >= "11/1/2006" And Fdate <= "11/7/2006" Then
looks like it should be more like:
If Fdate >= dateserial(2006,11,1) And Fdate <= dateserial(2006,11,7) Then
 
R

Ron de Bruin

Hi Dave/Dave

On my US machine both give the same result but on my Dutch machine
with the Dutch date format not (dmy)

You must use dateserial like Dave posted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top