Rename files in a folder using VBA

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
Hi everyone,

Can someone show me the code to rename the files in a folder?

Renaming is simply adding a fixed text string "IMG_" to the existing filename. It should replace the existing file (in the same folder).

Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Use the Name statement in a Dir function loop. There should be code examples in this forum.
 
Upvote 0
I presume you've got a lot of file names. 10, 100 or 1000?
- Start up command prompt (cmd.exe) and run as administrator.
- Go to directory where your files are. Let's presume C:\temp
- Type dir /b
- Look for the little black icon at the top left
- Choose Edit | Mark. You can now select your files.
- Choose Edit | Copy
- Go to Excel and start up a new workbook and select A1
- Hit Paste

Now suppose your file name is something like: Fortitude.S01E01.WEB-DL.XviD-FUM.jpg and you want to replace the part "WEB-DL.XviD-FUM" with "IMG_" (without quotes.)
- Go to B1 and enter formula =SUBSTITUTE(A1;"WEB-DL.XviD-FUM.jpg";"IMG_.jpg") Attention, I use semi colon and not comma because I have Dutch version.
- Copy down
- Now you have your correct file names in B1

- Copy and Paste this code in a new module -> Alt+F11 | Insert | Module

Code:
Sub RenameFiles()
Dim xDir As String
Dim xFile As String
Dim xRow As Long
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        If .Show = -1 Then
            xDir = .SelectedItems(1)
            xFile = Dir(xDir & Application.PathSeparator & "*")
            Do Until xFile = ""
                xRow = 0
                On Error Resume Next
                xRow = Application.Match(xFile, Range("A:A"), 0)
                If xRow > 0 Then
                    Name xDir & Application.PathSeparator & xFile As _
                    xDir & Application.PathSeparator & Cells(xRow, "B").Value
                End If
                xFile = Dir
            Loop
        End If
    End With
End Sub

- Run the code by hitting F5
When you are prompted to select the correct directory, select C:\temp (when your files are in that directory!)
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,877
Members
452,949
Latest member
Dupuhini

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