PaulskinX1
New Member
- Joined
- Mar 28, 2021
- Messages
- 12
- Office Version
- 365
- Platform
- Windows
Hello and thanks for reading,
I am trying to rename slightly the name of each file in a specific folder. I found online a bit of code which I re-adjusted slightly to get the below:
Sub ChangeFileName2()
Dim folderName As String
Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim FSOFile As Object
Dim NewFile As String
Dim CurrentYear As String
'Set the file name to a variable
folderName = "C:\Users\PROJECTS 2021\HE & BBI\"
'Set all the references to the FSO Library
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
Set FSOFolder = FSOLibrary.GetFolder(folderName)
Set FSOFile = FSOFolder.Files
CurrentYear = "2003"
'Use For Each loop to loop through each file in the folder
For Each FSOFile In FSOFile
NewFile = FSOFile.Name
NewFile = Replace(NewFile, "2002", CurrentYear)
Next
'Release the memory
Set FSOLibrary = Nothing
Set FSOFolder = Nothing
Set FSOFile = Nothing
End Sub
However it doesnt do anything - it doesnt change the name.
When I change NewFile = FSOFile.Name to FSOFile.Name = NewFile in For each loop , it produces error - stops on line FSOFile.Name = NewFile and the error is:
I am trying to rename slightly the name of each file in a specific folder. I found online a bit of code which I re-adjusted slightly to get the below:
Sub ChangeFileName2()
Dim folderName As String
Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim FSOFile As Object
Dim NewFile As String
Dim CurrentYear As String
'Set the file name to a variable
folderName = "C:\Users\PROJECTS 2021\HE & BBI\"
'Set all the references to the FSO Library
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
Set FSOFolder = FSOLibrary.GetFolder(folderName)
Set FSOFile = FSOFolder.Files
CurrentYear = "2003"
'Use For Each loop to loop through each file in the folder
For Each FSOFile In FSOFile
NewFile = FSOFile.Name
NewFile = Replace(NewFile, "2002", CurrentYear)
Next
'Release the memory
Set FSOLibrary = Nothing
Set FSOFolder = Nothing
Set FSOFile = Nothing
End Sub
However it doesnt do anything - it doesnt change the name.
When I change NewFile = FSOFile.Name to FSOFile.Name = NewFile in For each loop , it produces error - stops on line FSOFile.Name = NewFile and the error is: