我有一个替换文本文件中特定字符串的功能。这对于特定文件(test.conf)很好,没有任何问题。但是现在我正在寻找一种方法来检查目录中此字符串的每个文件。有没有一种方法可以检查目录中的所有文件,并找到并替换每个文件中的字符串?我不太确定如何使用“每个文件”功能。
Set objFile = objFSO.OpenTextFile(operations & "\test.conf", ForReading)
strText = objFile.ReadAll
objFile.Close
strText = Replace(strText, "$$Username:$$", username)
Set objFile = objFSO.OpenTextFile(operations & "\test.conf", ForWriting)
objFile.WriteLine strText
objFile.Close
这是我对特定文件“ test.conf”的解决方案。
但是此文件夹中可能包含更多带有$$Username:$$
字符串的文件。
示例:
\FOLDER\test.conf \FOLDER\abc.conf \FOLDER\def.conf ...
答案 0 :(得分:1)
尝试类似这样的方法循环遍历每个.conf文件:
Option Explicit
Const ForReading = 1
Const ForWriting = 2
dim sFolder : sFolder = "C:\Temp\"
dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
dim oFile, objFile, strText
For Each oFile In oFSO.GetFolder(sFolder).Files
If UCase(oFSO.GetExtensionName(oFile.Name)) = "CONF" Then
Set objFile = oFSO.OpenTextFile(oFile.Path, ForReading)
strText = objFile.ReadAll
objFile.Close
Set objFile = Nothing
strText = Replace(strText, "texttoreplace", "newtext")
Set objFile = oFSO.OpenTextFile(oFile.Path, ForWriting)
objFile.WriteLine strText
objFile.Close
Set objFile = Nothing
End if
Next
Set oFSO = Nothing