Text VBScript-如何在指定的行之后追加新行 < >我需要编辑一些配置文件,将新的行添加到现有文件中,但不是在文件的结尾,而是在中间的某个地方(在特定部分的结尾)

Text VBScript-如何在指定的行之后追加新行 < >我需要编辑一些配置文件,将新的行添加到现有文件中,但不是在文件的结尾,而是在中间的某个地方(在特定部分的结尾),text,vbscript,append,Text,Vbscript,Append,我是否应该像这里所描述的那样使用搜索和替换: 查找特定节的最后一行并将其替换为:自身+新行字符+我的新行=需要添加哪个 或 也许有一种更简单或更聪明的方法来做同样的事情(比如找到特定节的最后一行,并使用某种方法将我的新行放在后面)?因为您的任务是在节中附加一行,并且您的数据似乎表明节由两行结尾分隔,使用Split()在该分隔符上,不依赖于知道该部分的最后一个键值对,这看起来是一个很好的策略: Dim sAll : sAll = readAllFromFile("..\data\cfg00.

我是否应该像这里所描述的那样使用搜索和替换:

查找特定节的最后一行并将其替换为:自身+新行字符+我的新行=需要添加哪个


也许有一种更简单或更聪明的方法来做同样的事情(比如找到特定节的最后一行,并使用某种方法将我的新行放在后面)?

因为您的任务是在节中附加一行,并且您的数据似乎表明节由两行结尾分隔,使用Split()在该分隔符上,不依赖于知道该部分的最后一个键值对,这看起来是一个很好的策略:

  Dim sAll : sAll = readAllFromFile("..\data\cfg00.txt")
  WScript.Echo sAll
  Dim aSects : aSects = Split(sAll, vbCrLf & vbCrLf)
  aSects(1) = aSects(1) & vbCrLf & "fooA = added"
  sAll = Join(aSects, vbCrLf & vbCrLf)
  WScript.Echo "-----------------------"
  WScript.Echo sAll
输出:

=========================
# section 1 description
foo1 = bar1
foo2 = bar2

# section 2 description
foo3 = c:\bar.cfg

# section 3 description
foo4 = bar4

-----------------------
# section 1 description
foo1 = bar1
foo2 = bar2

# section 2 description
foo3 = c:\bar.cfg
fooA = added

# section 3 description
foo4 = bar4

=========================
=========================
# section 1 description
foo1 = bar1
foo2 = bar2

# section 2 description
foo3 = c:\bar.cfg

# section 3 description
foo4 = bar4

-----------------------
# section 1 description
foo1 = bar1
foo2 = bar2

# section 2 description
foo3 = c:\bar.cfg
fooA = added

# section 3 description
foo4 = bar4

=========================