View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Dave Shaw Dave Shaw is offline
external usenet poster
 
Posts: 21
Default Macro to copy files

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.


"Ron de Bruin" wrote:

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


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Ron de Bruin" wrote in message ...
Hi Dave

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


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Dave Shaw" wrote in message ...
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