1

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
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
Praveenks
  • 1,436
  • 9
  • 40
  • 79
  • do you use "On Error Resume Next" in your (full) code? Are you sure that you have read/write rights for the files? – Ekkehard.Horner Feb 21 '13 at 07:37
  • I refer you to [here](http://stackoverflow.com/a/13802548/111794) for instructions on using Visual Studio or Microsoft Script Editor as a WSH/VBScript debugger. – Zev Spitz Feb 21 '13 at 07:55
  • yes i have read/Write access to the files.. – Praveenks Feb 21 '13 at 08:27
  • I just debugged my code using visual studio and it is giving error-Microsoft VBScript runtime error: Name redefined: 'ForReading' – Praveenks Feb 21 '13 at 08:52
  • Just have to put const forReading=1 and forwriting=1 outside the loop(define at the beginning) so that it can't get defined everytime the loop runs...thanks for the resopnses – Praveenks Feb 21 '13 at 09:18

1 Answers1

1

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

Razvan
  • 36
  • 1
  • 3