Gary Dorman
Gary Dorman

Reputation: 477

Append Text to End of File Name for Multiple .xlsx Files in a Folder

I have a large amount of excel files in a folder and I need to append -MN to the end of all the file names. I've looked around the web for a solution but haven't had much luck finding a clear answer.

For example:

examplefile.xlsx would become examplefile -MN.xlsx

Any help would be greatly appreciated! Thank you!

Upvotes: 0

Views: 2310

Answers (2)

Hrothgar
Hrothgar

Reputation: 422

This should work. I changed If Right(myFileName, 5) = ".xlsx" Then to... If Right(myFileName, 4) = ".xlsx" Then

Sub RenameFiles()
Dim myFilePath As String, myFileName, NewFileName As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

myFilePath = "C:\Temp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(myFilePath)
For Each objFile In objFolder.Files
    myFileName = objFile.Name
    If Right(myFileName, 5) = ".xlsx" Then
        NewFileName = Replace(myFileName, ".xlsx", "-MN.xlsx")
        Name myFilePath & objFile.Name As myFilePath & NewFileName
    End If
Next objFile
End Sub

Upvotes: 1

ArindamD
ArindamD

Reputation: 241

  1. Go to command prompt
  2. Point to the folder location having the files
  3. Type: ren *.xlsx *-MN.xlsx

This should add the suffix (-MN) to all xlsx files present in the folder

Upvotes: 2

Related Questions