2013-02-21 99 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常量(在你發佈的代碼中)。只要刪除它們。

相關問題