VBA - Bulk Renaming of Files

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,

I'm currently working on a macro but can't find any that suits the process I have. I need to rename hundreds of files in word, excel and pdf format.

Example Filenames:

HOP12345 FileOne.doc
9876ABCD FileTwo 1234.xls
A1RBL234 Data 000.pdf

What I need is the macro that will rename the files by batch in a single folder. Basically, I just need to get the first 8 letter/word (mixed) to be the filename of each file.


Any help will be much appreciated. :)
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Maybe something like this...

Code:
[COLOR=darkblue]Sub[/COLOR] Renamer()
    [COLOR=darkblue]Dim[/COLOR] strPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], strFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], counter [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [B]strPath = "C:\Test\"    [COLOR=green]'Change the path to suit[/COLOR][/B]
    strFile = Dir(strPath & "*.*")
    [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] strFile <> ""
        [COLOR=darkblue]If[/COLOR] Len(strFile) > 12 [COLOR=darkblue]Then[/COLOR]
            Name strPath & strFile [COLOR=darkblue]As[/COLOR] strPath & Left(strFile, 8) & Mid(strFile, InStrRev(strFile, "."))
            counter = counter + 1
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        strFile = Dir
    [COLOR=darkblue]Loop[/COLOR]
    MsgBox counter & " files renamed.", vbInformation, "File Renaming Complete"
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
It works! Appreciate your help AlphaFrog.

By the way, how can you tweak the code if if going to get the mid string? :)

Examples:
0001_HOP12345_FileOne.doc
0002TH_9876ABCD_FileTwo.xls
NO0002_A1RBL234_Data000.pdf
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top