Hi Deepak , thank you for your solution , but in my case it is different,
It did not work for me .
So, Pls be specific & also share the sample xl with manual calculation/steps.
ie : "ID_2G_SCF_54545455455.xml" will be "ID_2G_SCF_20150519.xml "You mean to say that rename the last part of file name followed by "_" with YYYYMMDD
Option Explicit
Sub rename_2()
Dim OldName As String, NewName As String, mydir As String, r As Range
mydir = Application.ThisWorkbook.Path & "\"
For Each r In Range("A1:A10")
OldName = r
NewName = Left(r, Len(r) - InStrRev(r, "_")) & Format(Date, "YYYYMMDD")
Name mydir & OldName As mydir & NewName
Next
End Sub
Something like as ...
Code:Option Explicit Sub rename_2() Dim OldName As String, NewName As String, mydir As String, r As Range mydir = Application.ThisWorkbook.Path & "\" For Each r In Range("A1:A10") OldName = r NewName = Left(r, Len(r) - InStrRev(r, "_")) & Format(Date, "YYYYMMDD") Name mydir & OldName As mydir & NewName Next End Sub
I said like something not copy paste & run the code.
Pls wait for sometimes, i will get back to you with copy-paste code.
Option Explicit
Sub rename_3()
Dim NewName As String, mydir As String, objFile As Object
mydir = Application.ThisWorkbook.Path
With CreateObject("Scripting.FileSystemObject")
For Each objFile In .GetFolder(mydir).Files
If InStrRev(objFile.Name, "_") Then
NewName = Replace(objFile.Name, Mid(objFile.Name, _
InStrRev(objFile.Name, "_") + 1, InStrRev(objFile.Name, ".") - _
InStrRev(objFile.Name, "_") - 1), Format(Date, "YYYYMMDD"))
Name objFile As Replace(objFile, objFile.Name, NewName)
End If
Next
End With
MsgBox "Done"
End Sub
Check this...
Code:Option Explicit Sub rename_3() Dim NewName As String, mydir As String, objFile As Object mydir = Application.ThisWorkbook.Path With CreateObject("Scripting.FileSystemObject") For Each objFile In .GetFolder(mydir).Files If InStrRev(objFile.Name, "_") Then NewName = Replace(objFile.Name, Mid(objFile.Name, _ InStrRev(objFile.Name, "_") + 1, InStrRev(objFile.Name, ".") - _ InStrRev(objFile.Name, "_") - 1), Format(Date, "YYYYMMDD")) Name objFile As Replace(objFile, objFile.Name, NewName) End If Next End With MsgBox "Done" End Sub
Hi Deepack , only work with one file
thank you again
You might did something wrong as i have tested before posting.
pls share the before and after screenshot.
Hi Deepak, please read this doc that i've write to you
Option Explicit
Sub rename_4()
Dim NewName As String, mydir As String, objFile As Object
Dim Last_name As String, id As String
mydir = Application.ThisWorkbook.Path
id = Range("e2").Value
With CreateObject("Scripting.FileSystemObject")
For Each objFile In .GetFolder(mydir).Files
If Not InStrRev(objFile.Name, "_") Then GoTo N
Last_name = Mid(objFile.Name, _
InStrRev(objFile.Name, "_") + 1, InStrRev(objFile.Name, ".") - _
InStrRev(objFile.Name, "_") - 1)
If IsNumeric(Last_name) Then
NewName = Replace(objFile.Name, Last_name, Format(Date, "YYYYMMDD"))
Else
NewName = Replace(objFile.Name, Last_name, Last_name & "_" & Format(Date, "YYYYMMDD"))
End If
NewName = Replace(NewName, id, "")
NewName = id & "_" & NewName
Name objFile As Replace(objFile, objFile.Name, NewName)
N: Next
End With
MsgBox "Done"
End Sub
Hi, Deepak,please read this doc that I wrote for you.Pls take penalty to paste here names of before and after.
Hi, Deepak,please read this doc that I wrote for you.
I have asked names not picture..
Like as
Before - After as per you without any macro.