我必须使用新值i,e更新test.txt。删除值{“AAA”:“777”,“BBB”:“888”}并在标签中添加{“ABC:”123“,”GHN:“246”} 但是我的脚本将值附加为{“AAA”:“777”,“BBB”:“888”} {“ABC:”123“,”GHN:“246”
strFile="test.txt"
Const ForAppending = 8
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(strFile, ForAppending , True)
objFile.Write("{")
Dim dict
Set dict= CreateObject("Scripting.Dictionary")
dict.Add "ABC","123"
dict.Add "GHN","246"
k=dict.Keys
v=dict.Items
For j=0 to dict.Count-1
objFile.Write(chr(34)&k(j)&":"&chr(34)&v(j)&chr(34)&",")
Next
==============
Test.txt的
[Valid]
api = https://test.com
Alpha = FALSE
tag = {"AAA":"777" , "BBB":"888"}
答案 0 :(得分:0)
我从来没有使用过字典对象/不知道怎么做,所以根据你是否需要对象提供的某些东西,这对你来说可能不是一个可行的解决方案......但是如果不使用正则表达式,你能不能不是简单地做这样的事情吗?
Dim fso, filecontent, file
Set fso = CreateObject("Scripting.FileSystemObject")
file = "C:\temp\test.txt"
filecontent = fso.OpenTextFile(file, 1, False).ReadAll
filecontent = Replace(filecontent, "{""AAA"":""777"" , ""BBB"":""888""}", "{""ABC:""123"",""GHN:""246""}")
fso.OpenTextFile(file, 2, True).Write filecontent