Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-16-2021, 04:40 PM
Jayay159 Jayay159 is offline Rename Files in a Folder and Overwrite if File Exists Windows 10 Rename Files in a Folder and Overwrite if File Exists Office 2013
Novice
Rename Files in a Folder and Overwrite if File Exists
 
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
  #2  
Old 10-19-2021, 11:20 PM
Peterson Peterson is offline Rename Files in a Folder and Overwrite if File Exists Windows 10 Rename Files in a Folder and Overwrite if File Exists Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

Double post...
Reply With Quote
  #3  
Old 10-19-2021, 11:28 PM
Peterson Peterson is offline Rename Files in a Folder and Overwrite if File Exists Windows 10 Rename Files in a Folder and Overwrite if File Exists Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

Before running the code, click Tools > References… in the Visual Basic Editor and make sure that “Microsoft Scripting Runtime” is checked.


Code:
Sub RenameFile()
' Before running the code, click Tools > References in the Visual Basic Editor and 
' make sure that "Microsoft Scripting Runtime" is checked.
   
    Dim strPath As String
    Dim objFile As File
    Dim objFolder As Folder
    Dim objFSO As Scripting.FileSystemObject
    
    ' Put your path here:
    strPath = "C:\DOCUMENTS\FOLDER"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(strPath) Then
        
        Set objFolder = objFSO.GetFolder(strPath)
        
        ' Loop through all files in the folder:
        For Each objFile In objFSO.GetFolder(strPath).Files
            ' If the file starts with the letter I and is a Word file...
            If InStr(objFile.Name, "I") = 1 And InStr(objFile.Name, ".docx") <> 0 Then
                '...then copy it with the I removed from the name, overwriting
                ' an existing version, if necessary:
                objFSO.CopyFile objFolder.path & "\" & objFile.Name, objFolder.path & "\" & Mid(objFile.Name, 2), True
                ' Delete the original "I" version:
                objFSO.DeleteFile (objFile.path)
            End If
        Next objFile
    End If
End Sub
Reply With Quote
  #4  
Old 10-20-2021, 11:48 PM
Jayay159 Jayay159 is offline Rename Files in a Folder and Overwrite if File Exists Windows 10 Rename Files in a Folder and Overwrite if File Exists Office 2013
Novice
Rename Files in a Folder and Overwrite if File Exists
 
Join Date: Oct 2021
Posts: 2
Jayay159 is on a distinguished road
Default

Thank you Peterson, this is exaclty what I need.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Handler If User Does Not Want to Overwrite File ChrisOK Excel Programming 4 04-03-2020 08:31 PM
Need excel macro which helps to rename the files within the folder ganesang Excel Programming 1 02-04-2019 11:13 PM
need to overwrite folder permissions from previous office version gmee Outlook 0 02-19-2018 10:06 AM
Rename Files in a Folder and Overwrite if File Exists Normal file already exists cryptogram Word 2 09-21-2015 08:56 PM
Word does not ask do i want to overwrite a file on saving a file sam2149 Word 2 03-24-2014 04:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:55 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft