The below mentioned code works perfectly fine whenever I move the files from one folder to another. However, it stops if file is already available in the destination folder. Can anyone please help so that if the file with same file comes it should kill the previous file and replace it with the new one.
Sub MoveFilesAccordingFileType()
Dim fso As Scripting.FileSystemObject
Dim Fl As Scripting.File
Dim sourceFldr As Scripting.Folder
Dim DestinationFldr As Scripting.Folder
Set fso = New FileSystemObject
Set sourceFldr = fso.GetFolder("E:\Deploy_The_Process\Add Limits")
Set DestinationFldr = fso.GetFolder("E:\Deploy_The_Process\Archive\Word")
For Each Fl In sourceFldr.Files
If (fso.GetExtensionName(Fl.Path) = "docx") Then
fso.MoveFile Fl.Path, DestinationFldr.Path & "/"
End If
Next Fl
End Sub
I will be thankful for this
CodePudding user response:
You probably just want your loop to have a conditional kill line like so:
For Each Fl In sourceFldr.Files
If (fso.GetExtensionName(Fl.Path) = "docx") Then
If Dir(DestinationFldr.Path & "/" & Fl.Name) <> "" Then Kill DestinationFldr.Path & "/" & Fl.Name
fso.MoveFile Fl.Path, DestinationFldr.Path & "/"
End If
Next Fl