1

I have a collection of files (jpg, zip, xlsm, all kinds of different formats) in a specific folder which all have the string "MONTH" at the end of their file names.

I need to loop through each file in the folder replace "MONTH" with the contents of an Inputbox, allowing a user to enter any month they want.

I created the following sub, and although it doesn't return any errors, the file names aren't changing. Can't seem to figure out where I'm going wrong, help?

Sub FileNameAddMonth()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("C:\User\Desktop\MonthlyReports")
Dim Currentmonth As String
Currentmonth = InputBox("Please input the current month")

For Each file In Folder.Files
        sNewFile = file.Name
        sNewFile = Replace(sNewFile, "MONTH", Currentmonth)
Next
End Sub
  • Possible duplicate of [rename a file using VBScript](https://stackoverflow.com/questions/17660117/rename-a-file-using-vbscript) – Comintern Aug 02 '18 at 00:45

1 Answers1

0

Add file.name = sNewFile in the for-each loop.

PaichengWu
  • 2,649
  • 1
  • 14
  • 28