2013-02-21 139 views
1

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

你在你的(完整)代码中使用“On Error Resume Next”吗?你确定你有文件的读/写权限吗? – 2013-02-21 07:37:19

+0

有关使用Visual Studio或Microsoft脚本编辑器作为WSH/VBScript调试器的说明,请参阅[这里](http://stackoverflow.com/a/13802548/111794)。 – 2013-02-21 07:55:36

+0

是的,我已经读取/写入文件.. – Praveenks 2013-02-21 08:27:49

回答

1

在我看来,你并没有使用ForReading和ForWriting常量(在你发布的代码中)。只要删除它们。

相关问题