Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursively Replace String in Folder Names 1

Status
Not open for further replies.

GeorgeEdward

Technical User
Nov 15, 2013
3
AU
Hello

I am trying to walk through a directory structure and remove the apostrophe or single quote "'" character in folder names (not file names).

For example the folder "Budget's" would become "Budgets".

I am having trouble with the replace part of my code below! If you have any suggestions I would be most grateful.

Regards

GE [cat2]

Sub FolderReplace1()

Dim strFolder As String
strFolder = Me.MainFolder
Call FolderReplace2(strFolder)

End Sub

Sub FolderReplace2(strFolder)

Dim fso As New FileSystemObject
Dim objSubFolders
Dim objFolder

Set objSubFolders = fso.GetFolder(strFolder).SubFolders
For Each objFolder In objSubFolders
fso.MoveFile objFolder.Path, Replace(objFolder.Path, Chr(39), Chr(32))
FolderReplace2 objFolder.Path
Next

Set fso = Nothing

End Sub
 
I am having trouble
Well, which trouble ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi Patricia

Thankyou for the forum FAQ links.

Specifically I receive Run-time error 53: File not found, which I know relates to the line below:

Code:
fso.MoveFile objFolder.Path, Replace(objFolder.Path, Chr(39), Chr(32))

The recursive code works fine its just I'm sure how to use the fso.move to replace a character in an existing folder name.

Regards

GE
 
What about this instead ?
Code:
Sub FolderReplace2(strFolder)
Dim fso As New FileSystemObject
Dim objSubFolders
Dim objFolder
Dim strOldPath, strNewPath
Set objSubFolders = fso.GetFolder(strFolder).SubFolders
For Each objFolder In objSubFolders
    strOldPath = objFolder.Name
    strNewPath = Replace(strOldPath, Chr(39), Chr(32))
    If strNewPath <> strOldPath Then
        objFolder.Name = strNewPath
    End If
    FolderReplace2 objFolder.Path
Next
Set fso = Nothing
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi Patricia
Your changes worked perfectly. Thankyou so much!
Regards
GE [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top