替换子文件夹中每个文件中的一些文本

时间:2013-02-21 06:59:52

标签: vbscript replace text-files

我在一个文件夹中有大约30个子文件夹(每个文件夹包含许多子文件夹)。我正在尝试从每个sql文件中替换一些文本。我在vbscript中编写了一些代码并使用3-4个子文件夹对其进行测试当我试图用所有文件夹运行它时,文件没有被写入。

'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

1 个答案:

答案 0 :(得分:1)

在我看来,无论如何你都没有使用For Reading和For Constant(在你发布的代码中)。只需删除它们。