编辑文件夹中的每个文件

我有一个替换文本文件中特定字符串的功能。这对于特定文件(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
...
luchangshuai1989 回答:编辑文件夹中的每个文件

尝试类似这样的方法循环遍历每个.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
本文链接:https://www.f2er.com/3137441.html

大家都在问