View Single Post
 
Old 10-19-2021, 11:28 PM
Peterson Peterson is offline Windows 10 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