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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
See if solve your problem
 
Upvote 0
Solution
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
 
Upvote 0
Try change
FileCopy SourceFolder & f, FolderB & f
to
FileCopy SourceFolder & f, FolderC & f
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,220
Members
448,876
Latest member
Solitario

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