Praveenks
Praveenks

Reputation: 1496

Replace some text in each single file inside subfolders

I have around 30 subfolders(each folder consists many subfolders) in a folder.I am trying to replace some text form each sql file.I have written some code to this in vbscript and test it with 3-4 subfolders and it was working perfectly.But when i am trying to run it with all the folders, the files are not getting written.

'Root path where all folders and files are present
'Change according to your requirement

strPath="W:\New Folder\Test1"

Set objFso = CreateObject("Scripting.FileSystemObject")

'To access folders
Set objFolder = objFso.GetFolder (strPath)

TraverseFolder (objFso.GetFolder(strPath))

Function TraverseFolder(FolderName)
    For Each fld in FolderName.SubFolders
        TraverseFolder(fld) 
        For Each flname in fld.Files 
            if objFso.GetExtensionName(flname.Path)="sql" then
                'msgbox fld.Path & "\" & objFso.GetFileName(flname.Path)

'After commenting whole below section,and running rest of code with
'the above mentioned msgbox every single folder and files are getting
'fetched but when i  uncomment below section, only some folders and
'files are getting displayed in msgbox'

                Const ForReading = 1
                Const ForWriting = 2

                Set objFile = objFso.OpenTextFile(fld.Path & "\" & objFso.GetFileName(flname.Path), 1)

                strText = objFile.ReadAll
                objFile.Close

                strText= Replace(strText, "A_", "L_")
                strText= Replace(strText, "A", "D")
                strText= Replace(strText, "Database\Sy", "Database\SQ")

                Set objFile = objFso.OpenTextFile(fld.Path & "\" & objFso.GetFileName(flname.Path), 2)

                objFile.WriteLine strText
                objFile.Close
            End If
        Next
    Next
End Function

Upvotes: 1

Views: 1729

Answers (1)

Razvan
Razvan

Reputation: 36

It seems to me that you are not using ForReading and ForWriting constants anyway(in the code you posted). Just delete them.

Upvotes: 1

Related Questions