I tried to rename all the files recursively in the folder through VBA.
Sub FileNameCleaner()
'***************************************************************************
'Purpose: Update the filename to the latest valuation date.
'Author: Justin Sun
'Date: July 12th, 2018
'Inputs: nil
'Outputs: nil
'***************************************************************************
Dim valdate, prvdate, myline As String
valdate = "1811"
prvdate = "1810"
myline = "&& for /r %i in (*" & prvdate & "*) do ren ""%i"" ""*" & valdate & "*"" "
mydate = "C:\Users\j1sunx\Desktop\test\"
Call Shell("cmd /k cd " & mydate & myline, vbNormalNoFocus)
End Sub
The logic is quite straight-forward, use the wildcard to replace the previous month to the current month. The method works fine for other month, but it fails at month 11. The code shown above tried to update the date from 1810 to 1811. But in my testcase. It changes my filename 201810.txt
to 20181811.txt
.
Thanks in advance for your help.