Rename Files based on folder name

Status
Not open for further replies.

sanket_sk

Board Regular
Joined
Dec 27, 2016
Messages
140
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Looking for a unique solution, don’t know whether it is possible.

I have so many folders with employee names containing multiple files (Jan, Feb, Mar, and so on) in it.

I am looking for VBA code that will first read the folder name then go inside the folder and rename files ( Add prefix of Folder name) .

For example

Folder name – Sanket , Santosh, Zakir

Files inside the folder – Jan , Feb, Mar


VBA to help me rename files as – Sanket-Jan, Sanket-Feb, Sanket-Mar, Santosh-Jan, Santosh-Feb and so on.


Once this is done VBA to help me copy renamed files from respective folders and paste them in a separate folder called “Master”


Note:- File extension can be .xlsx, .PDF, .jpg etc.


Could you please help develop this Macro?

Thanks & Regards,
Sanket
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try the following macro by setting your own folders in the code.
I wrote the descriptions, but let me know if you have any issues.

VBA Code:
Sub doIt()
' Add reference: Tools->References->Microsoft Scripting Runtime
Dim fso As FileSystemObject
Dim fldMain As Folder
Dim fldMaster As Folder
Dim fld As Folder
Dim fil As File

    Set fso = New FileSystemObject
   
    ' Main folder that contains the employee folders
    Set fldMain = fso.GetFolder("C:\Main")
   
    ' Master folder to copy the renamed files
    ' It doesn't have to be inside the Main folder
    Set fldMaster = fso.GetFolder("C:\Main\Master")
   
    ' Loop through employee folders
    For Each fld In fldMain.SubFolders
        ' Check the employee folder name and do not process folder named Master
        ' just in case you create the Master folder inside Main folder
        If fld.Name <> "Master" Then
            ' Loop through files in the employee folder
            For Each fil In fld.Files
                With fil
                    ' Rename the file in the employee folder
                    .Move fld.Path & Application.PathSeparator & fld.Name & "-" & .Name
                    ' Copy the renamed file into the Master folder
                    .Copy fldMaster.Path & Application.PathSeparator & fil.Name
                End With
            Next fil
        End If
    Next fld
End Sub

Edit: strNewFileName variable was not necessary, so I removed it.
 
Last edited:
Upvote 0
Solution
Try the following macro by setting your own folders in the code.
I wrote the descriptions, but let me know if you have any issues.

VBA Code:
Sub doIt()
' Add reference: Tools->References->Microsoft Scripting Runtime
Dim fso As FileSystemObject
Dim fldMain As Folder
Dim fldMaster As Folder
Dim fld As Folder
Dim fil As File
Dim strnewFileName As String

    Set fso = New FileSystemObject
   
    ' Main folder that contains the employee folders
    Set fldMain = fso.GetFolder("C:\Main")
   
    ' Master folder to copy the renamed files
    ' It doesn't have to be inside the Main folder
    Set fldMaster = fso.GetFolder("C:\Main\Master")
   
    ' Loop through employee folders
    For Each fld In fldMain.SubFolders
        ' Check the employee folder name and do not process folder named Master
        ' just in case you create the Master folder inside Main folder
        If fld.Name <> "Master" Then
            ' Loop through files in the employee folder
            For Each fil In fld.Files
                With fil
                    ' Rename the file in the employee folder
                    .Move fld.Path & Application.PathSeparator & fld.Name & "-" & .Name
                    ' Copy the renamed file into the Master folder
                    .Copy fldMaster.Path & Application.PathSeparator & fil.Name
                End With
            Next fil
        End If
    Next fld
End Sub

Thanks A lot for quick solution -

Its amazing !!!

Sanket
 
Upvote 0
You’re welcome.
Great to hear it helps.
Hi, can you please help me :( I am very new to vba

What if I have folders name Sanket , Santosh, Zakir
Inside each of these folders above there is a "result" folder
Inside the result folder there is one "salary.pdf" file
I would like to rename the .pdf file inside the result folder into Sanket-salary, Santosh-salary, Zakir-salary,....etc
Then copy all those new renamed file into a master folder.

Is it possible to do that? How do I code?

Thank you in advance!! I really need your help
 
Upvote 0
What if I have folders name Sanket , Santosh, Zakir
Inside each of these folders above there is a "result" folder
Inside the result folder there is one "salary.pdf" file
I would like to rename the .pdf file inside the result folder into Sanket-salary, Santosh-salary, Zakir-salary,....etc
Then copy all those new renamed file into a master folder.
Welcome to the MrExcel board!
Duplicate to: Rename files based on main folders name

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this older thread so please continue in the linked one. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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