0

I would like to loop through all of my subfolders and rename them to "example". I don't know the folder names of all my subfolders. My code does not work at all.

Example of Folder Structure:

Main Folder >Subfolder A > Subfolder 1 (Rename this)
                >Some file
            >Subfolder B > Subfolder 2 (Rename this)
                >Some file
            >...

Here's the code:

    Dim FSO As Object
    Dim folder As Object
    Dim subfolders As Object
    Dim MyFile As String
    Dim wb As Workbook
    Dim CurrFile As Object
    Dim NewFolderName As String
    NewFolderName = "Photos"

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set folder = FSO.GetFolder("C:\Users\Tom\Desktop\Test\SHeets\")
    Set subfolders = folder.subfolders


        For Each folder In folder.subfolders

        subfoldersName = folder.Path
        Application.StatusBar = subfoldersName


        Name subfoldersName As FolderName & NewFolderName    ' change the folder name

        Next



    Set FSO = Nothing
    Set folder = Nothing
    Set subfolders = Nothing

With Application
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
End With

End Sub
bummi
  • 27,123
  • 14
  • 62
  • 101
Tom
  • 1
  • 2
  • 3
    Can you please post the Code you wrote? – shahkalpesh Apr 30 '19 at 08:11
  • Your code attempts to rename all subfolders to "Photos" - which will fail after the first one because you can't have two folders with the same name. Also foldername is not declared or set so is null. If you had used Option Explicit you would have caught that. – Harassed Dad Apr 30 '19 at 10:03
  • Yes my code doesn't work and im asking for an elegant solution here, because i can not figure it out. – Tom Apr 30 '19 at 10:12
  • 1
    You should not remove your code from the post. You can ask to help with issues with your code, not to ask to write an entire new code for you – Tim Stack Apr 30 '19 at 10:19
  • What do you mean by "doesn't work"? What are the specific problems? If your problem is that you are not drilling down far enough, you can use a recursive routine. If it is something else, please enlighten us. – Ron Rosenfeld Apr 30 '19 at 10:55

0 Answers0