Solved

# Rename folders in VBA

Posted on 2013-01-03
4,143 Views
Hi,
I need help writting code in vba access .
The code should search directory for folders named "name" and change those folders name into "names"

thanks
0
Question by:maximyshka

LVL 13

Expert Comment

ID: 38740546
0

LVL 74

Expert Comment

ID: 38741694
This will work without the need for external libraries:

Dim strOriginalName As String
Dim strNewName As String

strOriginalName = "C:\Junk"
strNewName = "C:\JunkNew"

Name strOriginalName As strNewName

JeffCoachman
0

LVL 4

Accepted Solution

theGove earned 500 total points
ID: 38742225
From your post, it seems that you want to start at a particular folder and then search that folder and all of its sub folders to for any folder named "name" and change the name of that folder to "names", right?

This problem is not so easy because it requires you to iterate across folders at the same time as you are changing the names of those folders.  To do this well, I think you need a recursive call to a procedure that iterates across all the folders in the list and does the renaming, however, you have to be sure to iterate deep before you do any renaming so that you don't rename a folder that still needs to have its sub folders checked.

The following code should do the trick.  You'll need to put this code into a new module in Access.
Dim objFSO As Object

Sub ChangeName()
Set objFSO = CreateObject("Scripting.FileSystemObject")
renameFolders "c:\path\to\start\folder"
End Sub

Sub renameFolders(path As String)
Dim folder As Object
For Each folder In objFSO.GetFolder(path).SubFolders
renameFolders folder.path
If LCase(folder.name) = "name" Then
Debug.Print folder.path
Name folder.path As folder.path & "s"
End If
Next
End Sub

Change "c:\path\to\start\folder" to the real path where you want the program to begin and then run the procedure named "ChangeName".  It should do the trick.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …