Vbscript 编辑文件夹中的每个文件
我有一个函数来替换文本文件中的特定字符串。这对于特定的文件(test.conf)非常有效,没有任何问题。但是现在我正在寻找一种方法来检查目录中的每个文件中是否有这个字符串。是否有方法检查目录中的所有文件,并查找和替换每个文件中的字符串?我真的不知道如何为每个文件函数使用Vbscript 编辑文件夹中的每个文件,vbscript,Vbscript,我有一个函数来替换文本文件中的特定字符串。这对于特定的文件(test.conf)非常有效,没有任何问题。但是现在我正在寻找一种方法来检查目录中的每个文件中是否有这个字符串。是否有方法检查目录中的所有文件,并查找和替换每个文件中的字符串?我真的不知道如何为每个文件函数使用 Set objFile = objFSO.OpenTextFile(operations & "\test.conf", ForReading) strText = objFile.ReadAll objFile.Clo
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
...
\文件夹\test.conf
\文件夹\abc.conf
\文件夹\def.conf
...
尝试以下方法循环遍历每个.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
如有疑问,请阅读本手册。