I have a main folder, with many subfolders, and many files within those subfolders. I would like to add the suffix of the subfolder to the prefix of every file within the subfolder. I would then like to export all files within all subfolders, with the new prefix, to a new general folder to be bulk uploaded into a platform.
Before: C:\DESKTOP\EMPLOYEES\ is main folder, within this main folder there are many subfolders C:\DESKTOP\EMPLOYEES\JOHN_DOE_12345, within these subfolders I have many files C:\DESKTOP\EMPLOYEES\JOHN_DOE_12345\contract.pdf C:\DESKTOP\EMPLOYEES\JOHN_DOE_12345\confidentialityagreement.pdf ...
After:
C:\DESKTOP\EMPLOYEES\JOHN_DOE_12345
C:\DESKTOP\EMPLOYEES\JOHN_DOE_12345\12345_contract.pdf
C:\DESKTOP\EMPLOYEES\JOHN_DOE_12345\12345_confidentialityagreement.pdf
The number of characters will always vary for the subfolder names, but I figured I could somehow look specifically for the number after the second "_" to add as prefix.
I have VBA below that has been working great to at least rename the files with the folder name, but now I need to alter to only grab the number after the second "_" and also then move the files into a general folder.
Any help would be appreciated!
Below macro is used in Excel where in cell A7 I have the main folder path.
Sub doIt()
' Add reference: Tools->References->Microsoft Scripting Runtime
Dim fso As FileSystemObject
Dim fldMain As Folder
Dim fld As Folder
Dim fil As File
Dim file_path As String
Set fso = New FileSystemObject
' Main folder that contains the subfolders
Set fldMain = fso.GetFolder(Range("A7").Value)
' Loop through subfolders
For Each fld In fldMain.SubFolders
' Loop through files in the subfolder
For Each fil In fld.Files
With fil
' Rename the file in the subfolder
.Move fld.Path & Application.PathSeparator & fld.Name & "_" & .Name
End With
Next fil
Next fld
End Sub