Solved

Rename folders in VBA

Posted on 2013-01-03
3
3,841 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
3 Comments
 
LVL 13

Expert Comment

by:Surone1
Comment Utility
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
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 500 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now