Trim File names in a folder using macro

Halley yenn

New Member
Joined
Mar 17, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

I have a folder with 70k pdf files with names as below:
AMP_12347890_12347890_Eligible_0908.pdf
ERP_13459000_13459000_Professional_8886.pdf(In these examples EMPID:13459000 and 12347890 are mentioned twice)
I need a macro to rename the above files by deleting one empid as below:
AMP_12347890_Eligible_0908.pdf
ERP_13459000_Professional_8886.
Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi, a demonstration as a VBA beginner starter :​
VBA Code:
Sub Demo1()
    Const D = "_", P = "D:\Tests4Noobs\"
      Dim F$, S$(), N&
          F = Dir(P & "*.pdf")
    While F > ""
          S = Split(F, D)
          If UBound(S) = 4 Then Name P & F As P & Replace(F, S(2) & D, ""): N = N + 1
          F = Dir
    Wend
        F = N & " files renamed"
        Application.Speech.Speak F, True
        MsgBox F, , "Done !"
End Sub
 
Upvote 0
Hi, a demonstration as a VBA beginner starter :​
VBA Code:
Sub Demo1()
    Const D = "_", P = "D:\Tests4Noobs\"
      Dim F$, S$(), N&
          F = Dir(P & "*.pdf")
    While F > ""
          S = Split(F, D)
          If UBound(S) = 4 Then Name P & F As P & Replace(F, S(2) & D, ""): N = N + 1
          F = Dir
    Wend
        F = N & " files renamed"
        Application.Speech.Speak F, True
        MsgBox F, , "Done !"
End Sub
Hey Marc,
It is running but not renaming anything!
 
Upvote 0
As it rocks on my side so have you forgotten to update the folder Path within the VBA procedure ?​
Or maybe your files have specific attributes ?​
Or maybe your files not match your initial post ?​
 
Upvote 0
As it rocks on my side so have you forgotten to update the folder Path within the VBA procedure ?​
Or maybe your files have specific attributes ?​
Or maybe your files not match your initial post ?​
can you try these:
AMR_2789034567_2789034567_DEL-Active_FY20performance_20200204.pdf
AMR_0000300976_0000300976_DEL-Active_FY20performance_20200204.pdf

Required result:
AMR_2789034567_DEL-Active_FY20performance_20200204.pdf
AMR_0000300976_DEL-Active_FY20performance_20200204.pdf
 
Upvote 0
It's what happens when the explanation does not match the real context … :rolleyes:
So you must amend the procedure like this :
Rich (BB code):
If UBound(S) = 5
 
Upvote 0
It's what happens when the explanation does not match the real context … :rolleyes:
So you must amend the procedure like this :
Rich (BB code):
If UBound(S) = 5
That is exactly what I did but the result is:
AMR_DEL-Active_FY20performance_20200204.pdf
AMR_DEL-Active_FY20performance_20200204.pdf
not
AMR_2789034567_DEL-Active_FY20performance_20200204.pdf
AMR_0000300976_DEL-Active_FY20performance_20200204.pdf
 
Upvote 0
Oh yes this is my bad this time so wait a little …​
 
Upvote 0
My revised demonstration :​
VBA Code:
Sub Demo1r()
    Const D = "_", P = "D:\Tests4Noobs\"
      Dim F$, S$(), N&
          F = Dir(P & "*.pdf")
    While F > ""
          S = Split(F, D)
        If UBound(S) = 5 Then
            S(2) = Empty
            Name P & F As P & Replace(Join(S, D), D & D, D)
            N = N + 1
        End If
          F = Dir
    Wend
        F = N & " files renamed"
        Application.Speech.Speak F, True
        MsgBox F, , "Done !"
End Sub
 
Upvote 0
Solution
My revised demonstration :​
VBA Code:
Sub Demo1r()
    Const D = "_", P = "D:\Tests4Noobs\"
      Dim F$, S$(), N&
          F = Dir(P & "*.pdf")
    While F > ""
          S = Split(F, D)
        If UBound(S) = 5 Then
            S(2) = Empty
            Name P & F As P & Replace(Join(S, D), D & D, D)
            N = N + 1
        End If
          F = Dir
    Wend
        F = N & " files renamed"
        Application.Speech.Speak F, True
        MsgBox F, , "Done !"
End Sub
worked like a charm.?
Thanks Marc
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,018
Members
449,203
Latest member
tungnmqn90

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