search specific file name "common.txt" in folder & all subs , to replace specific text "xxxx/xxxx"

ethar1
ethar1 used Ask the Experts™
on
Dear all,
I need to write a VB script to search in specific folder and its sub folders about a file "common.txt" then replace any "xxxx/xxxx" with "yyy/yyy" inside it.

Thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
Hi, ethar1.

This should do it.
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Change the starting folder path on the next line'
ProcessFolder objFSO.GetFolder("c:\eeTesting")
Set objFSO = Nothing
WScript.Echo "All done"
WScript.Quit

Sub ProcessFolder(objFolder)
    Dim strFilename, strBuffer, objFile, objSubfolder
    strFilename = objFolder.Path & "\common.txt"
    If objFSO.FileExists(strFilename) Then
        Set objFile = objFSO.OpenTextFile(strFilename)
        strBuffer = objFile.ReadAll
        objFile.Close
        strBuffer = Replace(strBuffer, "xxxx/xxxx", "yyyy/yyyy")
        Set objFile = objFSO.CreateTextFile(strFilename, True)
        objFile.Write strBuffer
        objFile.Close
    End If
    For Each objSubfolder In objFolder.SubFolders
        ProcessFolder objSubfolder
    Next
    Set objFile = Nothing
    Set objSubfolder = Nothing
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial