这是我第一次使用VBScript。我正在尝试计算文本文件中的失败次数,并基于该值更新该值或执行其他代码块。我有“阅读”的工作和第二块代码的工作。但是,我无法使用更新的计数值更新文本文件。尝试使用Regex,但这只是将行清除为空白,而不是重写新行。
Dim FSO, ReadTextFile ' object
Dim Textline ' string/variant
Set FSO = CreateObject("Scripting.FileSystemObject")
path = "f:\users\me\FailureCounts.txt"
Set ReadTextFile = FSO.OpenTextFile(path, 1) ' Open text with read mode
Set WriteTextFile = FSO.OpenTextFile(path, 2) ' Open text with wrote mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "API_Failures") Then ' If textline contains 'API_Failures'
fail_count = Split(Textline, ",")(1) ' Split the textline with "," indicator to grab the count value
if fail_count <> 3 then
fail_count = fail_count + 1
set write_line = "API_Failures," & fail_count
Set re = New RegExp
re.Pattern = """API_Failures"": {[.]*?},"
count_text = re.Replace(TextLine, write_line)
WriteTextFile.Write(count_text)
End If
End If
Loop