Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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: 143
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
 



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:30 PM.


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