VBA to conditionally move files from one directory to another

JohanGduToit

Board Regular
Joined
Nov 12, 2021
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Greetings Experts,

Files are FTP'd into a 'New' folder and then moved from the 'New' folder into another folder named 'Images'; BUT only move a file if it does NOT ALREADY exist in the 'Images' folder. If a file already exist in the 'Images' folder, then delete it from the 'New' folder and move on to the next file.

Here is what I have... the code works fine; but I get an error 'File already exist' when there's a duplicated filename. So I am trying to move all files in folder 'New' into folder "Images"; but only if the files do not already exist in the destination folder. Once the code has looped through the entire 'New' directory, any remaining files in the 'New' folder can then be deleted. (These remaining files will already exist in the 'Images' folder, so I don't need them).

Any help would be most welcomed!

VBA Code:
Sub MoveFiles()

Dim NewFile As String

NewFile = Dir("D:\ParGo\PODS\IMAGES\NEW\*.*")

    Do Until NewFile = ""
        Name "D:\ParGo\PODS\IMAGES\NEW\" & NewFile As "D:\ParGo\PODS\IMAGES\" & NewFile
        NewFile = Dir
    Loop

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,224,987
Messages
6,182,161
Members
453,093
Latest member
Soffy

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