Using Excel VBA to rename files in a folder

SimonR

New Member
Joined
Mar 14, 2022
Messages
23
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi,

I am trying to come up with a code to rename hundreds of PDFs in a folder. I see this question has been asked a number of times before, but the requests have differed from mine, and I am not skilled enough in VBA to understand any of the scripts that have been posted and modify them accordingly. What's required is very simple in theory and should be possible in practice.

So, basically the files I'm trying to rename have long names starting with an 11-character alphanumeric string - a12345bcd67. I need to rename each file to the string "abcde123 " (single space) combined with the first 11 characters of the original file name, so for example "a12345bcd67xxxxxxxxxxxxxxxxxxx" becomes "abcde123 a12345bcd67". Can anyone advise?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
So, basically the files I'm trying to rename have long names starting with an 11-character alphanumeric string - a12345bcd67. I need to rename each file to the string "abcde123 " (single space) combined with the first 11 characters of the original file name, so for example "a12345bcd67xxxxxxxxxxxxxxxxxxx" becomes "abcde123 a12345bcd67". Can anyone advise?
This does not make much sense to me, as you would be naming ALL of the PDFs in that folder "abcde123 a12345bcd67", so each one would overwrite the previous, and you would be left with exactly one single PDF file in that folder named "abcde123 a12345bcd67".

That cannot be right, can it?
 
Upvote 0
This does not make much sense to me, as you would be naming ALL of the PDFs in that folder "abcde123 a12345bcd67", so each one would overwrite the previous, and you would be left with exactly one single PDF file in that folder named "abcde123 a12345bcd67".

No (sorry if I wasn't clear), only the first nine characters of each file should be the same. The second part is the first 11 characters taken from the original file name - it is going to be different in every case (this is work-related and I have simply renamed both parts to a generic alphanumeric string for security reasons).
 
Upvote 0
OK, I think I get it now. It doesn't matter what the current file name is, you just want to tack on this 9 character prefix to it.
So I modified the code found here (VBA Loop Through PDF Files in a Folder or Directory | VBAF1) to do what you want:
VBA Code:
Sub MyRenameFiles()
   
    'Variable Declaration
    Dim sFilePath As String
    Dim sFileName As String
    Dim prfx As String
    Dim oName As String
    Dim nName As String
       
    'Specify File Path
    sFilePath = "C:\Temp\Test"
   
    'Specify new prefix
    prfx = "abcde123 "
   
    'Check for back slash
    If Right(sFilePath, 1) <> "\" Then
        sFilePath = sFilePath & "\"
    End If
       
    sFileName = Dir(sFilePath & "*.pdf")
   
'   Loop through all PDF files in folder
    Do While Len(sFileName) > 0
        If (Right(sFileName, 3) = "pdf") And (Left(sFileName, Len(prfx)) <> prfx) Then
            'Get full path and name of file
            oName = sFilePath & sFileName
            'Build new path and name
            nName = sFilePath & prfx & sFileName
            'Rename file
            Name oName As nName
        End If
        'Set the fileName to the next available file
        sFileName = Dir
    Loop
   
    MsgBox "Macro complete!"
  
End Sub
Just change the file path and prefix values to suit your situation.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,912
Messages
6,175,346
Members
452,638
Latest member
Oluwabukunmi

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