?
Solved

Rename folders in VBA

Posted on 2013-01-03
3
Medium Priority
?
4,914 Views
Last Modified: 2013-01-10
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
Comment
Question by:maximyshka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Expert Comment

by:Surone1
ID: 38740546
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
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

by:
theGove earned 2000 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

Open in new window

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question