Solved Rename Folders in Bulk (1 Viewer)

jack555

Member
Local time
Today, 23:31
Joined
Apr 20, 2020
Messages
93
Have to rename the folders in a location. New name as per data in a table below.

Sample Data

Old NameNew Name
1234ABCD
2345EFGH
5678SCHO
8937MIBZ

I managed to get the code for renaming the folder, however, this works for one folder only. I want to rename the folders as above in one go. Please assist.

Code:
'VBA Rename a Folder
Sub VBAF1_Rename_Folder()
    
    'Variable declaration
    Dim sFolder_OldName As String
    Dim sFolder_NewName As String
    
    'Existing Folder Name
    sFolder_OldName = "C:\VBAF1\Sample\"
    
    'Define New Name of folder to change
    sFolder_NewName = "C:\VBAF1\Test\"
    
    'Check Specified folder exists or not
     If Dir(sFolder_OldName) <> "" Then
        'Rename Folder using Name function
        Name sFolder_OldName As sFolder_NewName
        MsgBox "Folder has renamed.", vbInformation, "VBAF1"
    Else
        MsgBox "Folder is not available to rename.", vbInformation, "VBAF1"
    End If
        
End Sub
 

Ranman256

Well-known member
Local time
Today, 15:31
Joined
Apr 9, 2015
Messages
4,339
Code:
set rst = currentdb.openrecordset("select * from tbl")
with rst
   while not .eof
      vOldName = .fields("oldName").value
      vNewName = .fields("NewName").value

   'run your rename code here

    .movenext    'get next record
  wend 
end with
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:31
Joined
Oct 29, 2018
Messages
21,453
How do you know what the old name was and what the new name needs to be? If you store them in a table, this would be possible using a loop.

Edit: Oops, too late...
 

jack555

Member
Local time
Today, 23:31
Joined
Apr 20, 2020
Messages
93
Code:
set rst = currentdb.openrecordset("select * from tbl")
with rst
   while not .eof
      vOldName = .fields("oldName").value
      vNewName = .fields("NewName").value

   'run your rename code here

    .movenext    'get next record
  wend
end with
Thanks for the instant reply. I used the code as below in a Form command button. However, no changes were made. Am I doing anything differently?


Code:
Private Sub Command36_Click()

'Variable declaration
    Dim sFolder_OldName As String
    Dim sFolder_NewName As String

set rst = currentdb.openrecordset("select * from tblNameChangeSource")
with rst
   while not .eof
      vOldName = .fields("oldName").value
      vNewName = .fields("NewName").value

   'run your rename code here
    'Existing Folder Name
    sFolder_OldName = "C:\VBAF1\vOldName\"
    
    'Define New Name of folder to change
    sFolder_NewName = "C:\VBAF1\vNewName\"
    
    'Check Specified folder exists or not
     If Dir(sFolder_OldName) <> "" Then
        'Rename Folder using Name function
        Name sFolder_OldName As sFolder_NewName
        MsgBox "Folder has renamed.", vbInformation, "VBAF1"
    Else
       ' MsgBox "Folder is not available to rename.", vbInformation, "VBAF1"
    End If

    .movenext    'get next record
  wend
end with
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:31
Joined
Feb 19, 2002
Messages
43,213
You never answered the question regarding how the code is supposed to know how to change the file names. These things don't happen by magic.
 

jack555

Member
Local time
Today, 23:31
Joined
Apr 20, 2020
Messages
93
You never answered the question regarding how the code is supposed to know how to change the file names. These things don't happen by magic.
the names to be changed in the given table below. Already included in my first question itself.

tblNameChangeSource
Old NameNew Name
1234ABCD
2345EFGH
5678SCHO
8937MIBZ
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:31
Joined
Feb 19, 2002
Messages
43,213
So you have FOUR files and you want them changed to the other four names. Why would you bother with code to do this? Here it is:)

Name "1234" As "ABCD"
Name "2345" As "EFGH"
Name "5678" As "SCHO"
Name "8937" As "MIBZ"
 

jack555

Member
Local time
Today, 23:31
Joined
Apr 20, 2020
Messages
93
So you have FOUR files and you want them changed to the other four names. Why would you bother with code to do this? Here it is:)

Name "1234" As "ABCD"
Name "2345" As "EFGH"
Name "5678" As "SCHO"
Name "8937" As "MIBZ"
These 4 are samples to understand the scenario. I have more than 2000 folders to rename in a short time.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:31
Joined
Feb 19, 2002
Messages
43,213
And we asked you HOW DO YOU GET FROM A TO B and you won't tell us. We can't make it up. ONLY YOU know the rule. You have code that loops through a folder. You have the "Name" command to do the rename. Write 2000 lines of code to hard code the names or come up with a formula to figure out how to get from a to b. You've given us nothing to work with.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:31
Joined
Feb 19, 2013
Messages
16,606
@Pat Hartman OP has told us in post #4 - oldname and new name are stored in a table. problem is their code is not working as required.

Looks like they got the code from here


but have not adapted it to meet their actual requirement which looks like it needs to be

Code:
'Existing Folder Name
    sFolder_OldName = "C:\VBAF1\vOldName\"
   
    'Define New Name of folder to change
    sFolder_NewName = "C:\VBAF1\vNewName\"

needs changing to

Code:
'Existing Folder Name
    sFolder_OldName = "C:\VBAF1\" & rst!OldName & "\"
   
    'Define New Name of folder to change
    sFolder_NewName = "C:\VBAF1\" & rst!NewName & "\"

although since the example code in the link uses a parent folder of 'C:\VBAF1\' I suspect this bit needs changing as well
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:31
Joined
Feb 19, 2002
Messages
43,213
He never answered the question about where the data was coming from. But I see now that I read the code, there is a table. I'll just go away and leave you to it:)
 

Users who are viewing this thread

Top Bottom