Using Excel VBA to rename files in directory

samilynn

Board Regular
Joined
Jun 24, 2003
Messages
171
Office Version
  1. 2016
Platform
  1. Windows
is it possible to have a spreadsheet with two columns, Col A showing a list of current file names in a particular directory, and Col B the names I want these files to be renamed to. Is there some code that I can use to do this, or do I have to rename these files one by one until I get old? :(

Thanks,

Samantha
 
When I replied to this thread some years ago I posted a short video showing how to enter the code in my posts. I have received a few automatic requests to re-share the video (evidently due to security updates this is necessary from time to time).
I have now re-shared the video if anyone wants it.
Scroll back through the thread to see the code.
Google Drive: Sign-in
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
When I replied to this thread some years ago I posted a short video showing how to enter the code in my posts. I have received a few automatic requests to re-share the video (evidently due to security updates this is necessary from time to time).
I have now re-shared the video if anyone wants it.
Scroll back through the thread to see the code.
Google Drive: Sign-in
Sorry that link might not work. Try this one.
 
Upvote 0
Hi, this is really similar to what i need in work, the only difference being the files that need renaming are in multiple folders and subfolders, is there any way the listfiles/filenametoexcel macro can be amended so you can select a directory/folder rather than an individual sheet? any help would be much appreciated, thanks
 
Upvote 0
i encounter run-time error '53' file not found when execute rename_files().When i click debug,it link me to

Name Cells(r,"A") As Cells(r,"B")

The issue is that the file path isn't included. Just change the ReName_Files macro to the below (and make sure to specify the path)

VBA Code:
Sub ReName_Files()

Dim r As Integer
[B]Dim path As String

path = "Y:\General\Test Path\" ' ---> Change to your path[/B]
r = 1
Do Until IsEmpty(Cells(r, "A")) Or IsEmpty(Cells(r, "B"))
Name [B]path &[/B] Cells(r, "A") As[B] path &[/B] Cells(r, "B")
r = r + 1
Loop
End Sub
 
Upvote 0
Hi, this is really similar to what i need in work, the only difference being the files that need renaming are in multiple folders and subfolders, is there any way the listfiles/filenametoexcel macro can be amended so you can select a directory/folder rather than an individual sheet? any help would be much appreciated, thanks

Not sure if this is helpful, but if you create an excel list you can just use the ReName_Files macro to rename, just make sure you include the path as part of the file name

So for example, if your excel has the below in it

Cell A1: C:\Users\Downloads\Test File.pdf
Cell B1: C:\Users\Downloads\This Is the same file.pdf

Your 'Test File.pdf' saved in your downloads folder would be renamed to 'This is the same file.pdf'


VBA Code:
Sub ReName_Files()
Dim r As Integer
r = 1
Do Until IsEmpty(Cells(r, "A")) Or IsEmpty(Cells(r, "B"))
Name Cells(r, "A") As Cells(r, "B")
r = r + 1
Loop
MsgBox "All your old file names in Column 'A' have been reNamed" & vbCr & _
        "to the adjacent new name in column 'B'."
End Sub
 
Upvote 1
Not sure if this is helpful, but if you create an excel list you can just use the ReName_Files macro to rename, just make sure you include the path as part of the file name

So for example, if your excel has the below in it

Cell A1: C:\Users\Downloads\Test File.pdf
Cell B1: C:\Users\Downloads\This Is the same file.pdf

Your 'Test File.pdf' saved in your downloads folder would be renamed to 'This is the same file.pdf'


VBA Code:
Sub ReName_Files()
Dim r As Integer
r = 1
Do Until IsEmpty(Cells(r, "A")) Or IsEmpty(Cells(r, "B"))
Name Cells(r, "A") As Cells(r, "B")
r = r + 1
Loop
MsgBox "All your old file names in Column 'A' have been reNamed" & vbCr & _
        "to the adjacent new name in column 'B'."
End Sub
Thats brilliant thank you, I will try this out!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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