Avatar of Gopinath dharmarajn
Gopinath dharmarajn
 asked on

Fine and Replace the string in the folder name and subfoldernames using INstr and replace function

Hi ,

i need the help for vba code using replace and Instr function


sub test()

dim filename As string
Dim filepath As String
Dim filerename AS string

filepath = "c:\"

filename = Instr(filepath,cert)

filerename = Replace(filename , "cert" , "cert1")


End sub
VBAVisual Basic Classic

Avatar of undefined
Last Comment
Shums Faruk

8/22/2022 - Mon
Shaun Vermaak

Try
filerename = filename.Replace("cert", "cert1")

Open in new window

Shums Faruk

You may try something like this:
Sub RenameFiles()
    Dim fs As FileSystemObject
    Dim fdBase As Folder
    Dim f As File
    Dim sFV As String
    Dim sTV As String
    Dim sPath As String
    Dim sName As String
    Dim sFullName As String
    sPath = "c:\"
    sFV = "cert"
    sTV = "cert1"
    Set fs = New FileSystemObject
    If Not fs.FolderExists(sPath) Then
        MsgBox "Invalid Path"
        Exit Sub
    End If
    Set fdBase = fs.GetFolder(sPath)
    For Each f In fdBase.Files
        sName = f.Name
        sName = Replace(sName, sFV, sTV)
        sFullName = f.ParentFolder & "\" & sName
        Name f As sFullName
    Next
    
    For Each fd In fdBase.SubFolders
        For Each f In fd.Files
            If f.Name Like "*" & sFV & "*" Then
                sName = f.Name
                sName = Replace(sName, sFV, sTV)
                sFullName = f.ParentFolder & "\" & sName
                Name f As sFullName
            End If
        Next
    Next
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Rgonzo1971

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Shums Faruk

No comment has been added to this question in more than 14 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:
Split: Rgonzo (ID: 42051107) and Shums (ID: 42051138)

If you feel this question should be closed differently, post an objection and a moderator will read all objections and then close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck