使用VBS替换多个文本文件中的文本

我正在尝试编写一个VBS脚本,该脚本将循环浏览多个文本文件,并用其他单词替换多个关键字,例如用GJA替换1ECODE。 我有一个工作代码,它将对单个文件执行此操作,代码中对此进行了详细说明,但无法使其在多个文本文件中循环。

我当前的代码(来自另一个线程)在下面,返回错误“对象不支持此属性或方法'f.FullName'

要编辑的文件全称为“ testfile_1.txt”,“ testfile_2.txt”等

任何人都可以提供的帮助将不胜感激

Set fso = CreateObject("Scripting.FileSystemObject")

Set prefixes = CreateObject("Scripting.Dictionary")
prefixes.CompareMode = vbTextCompare 'make dictionary lookups case-insensitive
prefixes.Add "testfile",True


For Each f In fso.GetFolder("C:\Users\msi\Documents\script_test\Test_folder").Files
  If InStr(f.Name,"_") > 0 Then
    If prefixes.Exists(Split(f.Name,"_")(0)) Then
      strText = fso.OpenTextFile(f.FullName).ReadAll

      strText = Replace(strText,"1ECODE","ELH")
      strText = Replace(strText,"1GCODE","GLH")
      strText = Replace(strText,"2ECODE","EQT")
      strText = Replace(strText,"2GCODE","GQT")
      strText = Replace(strText,"3ECODE","3GCODE","GQT")


 fso.OpenTextFile(f.FullName,2).Write strText
    End If
     End IF
 Next
LIUYAOZU 回答:使用VBS替换多个文本文件中的文本

File对象没有FullName属性,请改用Path

,

将代码更改为此,似乎可以按要求工作。

Const ForReading = 1
Const ForWriting = 2

Set FSO = CreateObject("Scripting.FileSystemObject")
set objFSO = CreateObject("Scripting.FileSystemObject")
  For Each f In       fso.GetFolder("C:\Users\ms\\Documents\Temporary_delete_every_month\script_test\Test_folder").Files

  msgbox f

set objFile = objFSO.OpenTextFile(f,ForReading)
StrText= objFile.ReadAll
ObjFile.close
Set objFile= nothing

strText = Replace(strText,"1ECODE","YYY")
strText = Replace(strText,"2ECODE","XXX")

set objFile = objFSO.OpenTextFile(f,ForWriting)

objFile.WriteLine strText

 objFile.Close

 Next
本文链接:https://www.f2er.com/3110183.html

大家都在问