我在一个文件夹中有大约 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