View Single Post
 
Old 10-16-2021, 04:40 PM
Jayay159 Jayay159 is offline Windows 10 Office 2013
Novice
 
Join Date: Oct 2021
Posts: 2
Jayay159 is on a distinguished road
Question Rename Files in a Folder and Overwrite if File Exists

Hi
I have a macro that renames all files in a folder that begin with the litter "I":

Sub RenameFile()

Dim file
Dim path As String

path = "C:\DOCUMENTS\FOLDER"
file = Dir(path & "I*.docx")

'rename the files with the I in front to remove the I
Dim NewName As String
Do While file <> ""
NewName = Mid(file, 2)
Name path & file As path & NewName

file = Dir()
Loop

End Sub

This works well and does what I expect, however, if the NewName file already exists, I just get an error that it already exists.
Can someone please help with extra code which will overwrite the NewName file if it already exists in the folder.

Thank you very much
Reply With Quote