Help edit code rename file for all subfolder!!!

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
180
Office Version
  1. 2016
Platform
  1. Windows
I have folder Image_Sonha include subforder as picture below.

1622172502127.png


And In subfolder : 20210406_18_001_HCG_DKT1_01, 20210406_18_002_HCG_DKT1_02, 20210406_18_003_HCG_DKT2_01, 20210406_18_004_HCG_DKT2_02 is have picture as below
1622172645976.png

i have code only repleace file G0019196_compressed->G0019196 and over write but only one folder. help edit code run for all subfolder in folder Image_SoNha
Code:
Sub Rename_overwrite()
Dim sPath As String, dPath As String, myOF As String
Dim lFor As String, ckLFor As Long
sPath = "C:\Users\dungna\Desktop\test\Image_SoNha\20210406_18_001_HCG_DKT1_01"     '<<< The start directory
dPath = "C:\Users\dungna\Desktop\test\Image_SoNha\20210406_18_001_HCG_DKT1_01"     '<<< The destination directory
lFor = "_compressed"              '<<< the Key to search
'
myOF = Dir(sPath & "*.JPG")
Do While myOF <> ""
    ckLFor = InStr(1, myOF, lFor, vbTextCompare)
    If ckLFor > 0 Then
        On Error Resume Next
        Kill dPath & Left(myOF, ckLFor - 1) & ".JPG"
        Name sPath & myOF As dPath & Left(myOF, ckLFor - 1) & ".JPG"
        On Error GoTo 0
    End If
    myOF = Dir
Loop
End Sub

Best regards,
Nguyen Anh Dung
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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