Automated checking if file exists in source folder & destination folder, If file does not exist in destination folder, copy it.

gishy

New Member
Joined
Apr 13, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I currently have this problem at hand. I need to check if my source folder and my destination folder has exactly the same files by comparing it.
If files that are present in the source folder is not present in my destination folder, the macro is able to copy and paste the file that is missing into my destination folder. I would need it to also be automated where I will be using a RunTimer function. Does anyone has any suggestions or a VBA code that is similar for me to look at? I am new to VBA and any help is greatly appreciated!

Thanks in advanced!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,171
Office Version
  1. 2016
Platform
  1. Windows
See if solve your problem
 
Solution

gishy

New Member
Joined
Apr 13, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
See if solve your problem

Hi it somewhat works! But I need some help in the modification.
I need the file that was missing to be copied into another separate folder e.g Folder C
So here is an overview:
Source Folder is compared with Folder B.
Missing files between source folder and Folder B will be copied into another Folder C.
End result is that Folder C contains the missing files between Source Folder & Folder B.
How do i modify the code so that the file can be copied into Folder C?
Thank you so much!

VBA Code:
Sub SyncFiles1()
  Dim f As String
  Const SourceFolder = "C:\Users\Desktop\Source\"
  Const FolderB = "C:\Users\Desktop\FolderB\"
  Const FolderC = "C:\Users\Desktop\FolderC\"
  f = Dir(SourceFolder & "*.txt")
  On Error Resume Next
  While Len(f)
    GetAttr FolderB & f
    If Err Then
      Err.Clear
      FileCopy SourceFolder & f, FolderB & f
    End If
    f = Dir()
  Wend
End Sub
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,171
Office Version
  1. 2016
Platform
  1. Windows
Try change
FileCopy SourceFolder & f, FolderB & f
to
FileCopy SourceFolder & f, FolderC & f
 

gishy

New Member
Joined
Apr 13, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Try change
FileCopy SourceFolder & f, FolderB & f
to
FileCopy SourceFolder & f, FolderC & f
Its working now! Thank you so much!!!
 

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,171
Office Version
  1. 2016
Platform
  1. Windows
Its working now! Thank you so much!!!
Since you are able to adapt from sample to your code, I can see that you can understand how the program flow. I guess you will slowly be able to modify on your own soon. (y)
Thanks for update
 

gishy

New Member
Joined
Apr 13, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Since you are able to adapt from sample to your code, I can see that you can understand how the program flow. I guess you will slowly be able to modify on your own soon. (y)
Thanks for update
Hi! If I want to add this part of the code in, how do I go about doing it? Thank you !
VBA Code:
GetAttr F1 & f
    If Err Then
      Err.Clear
      FileCopy SourceFolder & f, F1 & f

My code is currently this, basically I cant execute the portion that I want to execute which is the portion of the code above. (Which is comparing another 2 folders simultaneously and updating one of the folder)
VBA Code:
Option Explicit
Dim RunTimer As Date

Sub SyncFiles()
  Dim f As String
  Const SourceFolder = "C:\Users\Desktop\Source\"
  Const Final = "C:\Users\Desktop\FinalFolder\"
  Const F2 = "C:\Users\Desktop\F2\"
  Const F1 = "C:\Users\Desktop\F1\"
  f = Dir(SourceFolder & "*.txt")
  
  RunTimer = Now + TimeValue("00:10:00")

Application.OnTime RunTimer, "SyncFiles"
  
  On Error Resume Next
  While Len(f)
    GetAttr Final & f
    If Err Then
      Err.Clear
      FileCopy SourceFolder & f, F2 & f
      End If
    f = Dir()
  Wend
  While Len(f)
    GetAttr F1 & f
    If Err Then
      Err.Clear
      FileCopy SourceFolder & f, F1 & f
    End If
    f = Dir()
  Wend
End Sub

Sub StoptheCode()

Application.OnTime RunTimer, "SyncFiles", , False

MsgBox "The application has been stopped."
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,129,862
Messages
5,638,738
Members
417,049
Latest member
baka416

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
Top