我们有一个文件夹,里面有1000个记事本,里面有数据。我们试图在所有这些笔记本中找到并替换用户定义的字符串,而无需单独打开它们。可以帮助我使用vb脚本函数,这些函数向用户询问他想要替换哪个字符串以及应该替换哪些字符串然后替换所有笔记本中的那些同时出现。
答案 0 :(得分:1)
试试这个:
Option Explicit
Const TristateFalse = 0 ' ASCII
Const TristateTrue = -1 ' Unicode
Const TristateUseDefault = -2 ' System Default
Dim sFindExpr, sReplaceWith, sFolder, oFile, sContent
sFolder = "C:\Test\files"
sFindExpr = InputBox("Enter expression to be replaced")
sReplaceWith = InputBox("Enter expression to replace with")
With CreateObject("Scripting.FileSystemObject")
For Each oFile In .GetFolder(sFolder).Files
With .OpenTextFile(oFile.Path, 1, False, TristateUseDefault)
sContent = ""
If Not .AtEndOfStream Then sContent = .ReadAll
.Close
End With
sContent = Replace(sContent, sFindExpr, sReplaceWith)
With .OpenTextFile(oFile.Path, 2, False, TristateUseDefault)
.Write(sContent)
.Close
End With
Next
End With
CreateObject("WScript.Shell").PopUp "Completed", 1, , 64