Rename Files VBA

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Rename Files VBA

Post by santosm »

Hi All,
I have a folder with 800+ files in it. I need to rename in this fashion: 123456.abc to 12.3456.abc.

Is there a way to do this easily in VBA? I have read/write permission to the folder.

I am thinking I can open the folder as a list and step through, renaming each as I go along, just not quite sure how to tackle it.

Thanks,
Mark
Thanks,
Mark

User avatar
HansV
Administrator
Posts: 78621
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Rename Files VBA

Post by HansV »

Try this - you can call it from the On Click event procedure of a command button, or whichever way you want:

Code: Select all

Sub RenameFiles()
    ' Modify as needed but keep trailing backslash
    Const strFolder = "C:\MyFolder\"
    Dim strFile As String
    Dim strFileNew As String
    strFile = Dir(strFolder & "*.*")
    Do While strFile <> ""
        If Mid(strFile, 3, 1) <> "." Then
            strFileNew = Left(strFile, 2) & "." & Mid(strFile, 3)
            Name strFolder & strFile As strFolder & strFileNew
        End If
        Debug.Print strFile
        strFile = Dir
    Loop
End Sub
Best wishes,
Hans

santosm
3StarLounger
Posts: 253
Joined: 19 Apr 2010, 09:01
Location: Indiana, USA

Re: Rename Files VBA

Post by santosm »

Works dandy. Thanks!
Thanks,
Mark