Excel VBA Macro to synchronize Windows folders

Praedico

New Member
Joined
Jul 17, 2008
Messages
41
Working with Windows 7 and Office 2010

I've created a Excel workbook with VBA code to synchronize an Access database between a laptop and a server. One of the fields in the database is a shortcut to a jpg for each record. (The Access stuff is not too important here)

Now I'd like to add the ability synchronize the photos between the laptop and the server; so people who use the server can view the photos added by the laptop and vice versa.
Something like:

Code:
For each [JPG] in [LaptopFolder]
 If not [JPG] exists in [ServerFolder] then
  filecopy [LaptopFolder]\[JPG], [ServerFolder]\[JPG]
 end if
next [JPG]

Of course that code wouldn't work (that's why I'm here) it just gives you an idea of what I'm trying to accomplish.

Any nudge in the right direction would be much appreciated,
Thank you
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this:
Rich (BB code):
Sub SyncFiles()
  Dim f, Col As New Collection
  Const LaptopFolder = "C:\Temp\Pictures\"
  Const ServerFolder = "\\Server2\Pictures\"
  f = Dir(LaptopFolder & "*.jpg")
  While Len(f)
    Col.Add f
    f = Dir()
  Wend
  For Each f In Col
    If Len(Dir(ServerFolder & f)) = 0 Then FileCopy LaptopFolder & f, ServerFolder & f
  Next
End Sub
 
Upvote 0
This is better one:
Rich (BB code):
Sub SyncFiles1()
  Dim f As String
  Const LaptopFolder = "C:\Temp\Pictures\"
  Const ServerFolder = "\\Server2\Pictures\"
  f = Dir(LaptopFolder & "*.jpg")
  On Error Resume Next
  While Len(f)
    GetAttr ServerFolder & f
    If Err Then
      Err.Clear
      FileCopy LaptopFolder & f, ServerFolder & f
    End If
    f = Dir()
  Wend
End Sub
 
Upvote 0
I tried both and they both work. I agree the second one is better, the program only has to loop through each file once instead of twice, so I used it.
Thank you very much for your help.
 
Upvote 0
Glad it works for you, thanks for feedback!
 
Upvote 0
This is great, but what code could be used to synchronise two folders, keeping the newest version of files only. ie. the ones with the newest file date end up in both files with older versions being overwritten.

Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,213,565
Messages
6,114,337
Members
448,568
Latest member
Honeymonster123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top