的VBScript - 如何追加新的生产线后,指定行 [英] VBScript - how to append new line after line specified

查看:199
本文介绍了的VBScript - 如何追加新的生产线后,指定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过附加新行到现有文件编辑一些配置文件中,但不是在该文件的末尾,而是介于中间,而不是(在特定的部分的结尾)

I need to edit some configuration file by appending new line to existing file, but not at the end of the file but somewhere in the middle instead (at the end of particular section)

# section 1 description
foo1 = bar1
foo2 = bar2

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

# section 3 description
foo4 = bar4

我应该使用搜索和替换喜欢这里描述的:

Should I use search and replace like described here:

<一个href=\"http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/08/how-can-i-find-and-replace-text-in-a-text-file.aspx\" rel=\"nofollow\">http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/08/how-can-i-find-and-replace-text-in-a-text-file.aspx

要找到特定部分的最后一行,取而代之的是:自己+新行字符+ my_new_line = which_needs_to_be_appended

to find last line of particular section and replace it with: itself + new line character + my_new_line = which_needs_to_be_appended?

也许还有一个更简单或更聪明的方法做同样的事情(比如寻找特定部分的最后一行,并使用一些方法把我的新行权后)?

maybe there is a simpler or more clever method to do same thing (like finding the last line of particular section and use some method to put my new line right AFTER it)?

推荐答案

由于你的任务是追加一条线一个部分,你的数据似乎表明,部分由两个行尾分开,在该分离器使用斯普利特()貌似不依赖于知道的最后一个键 - 值对的那款好的战略:

As your task is to append a line to a section and your data seems to indicate that sections are separated by two line endings, using Split() on that separator looks like a good strategy that doesn't rely on knowing the last key-value-pair of that section:

  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

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

这篇关于的VBScript - 如何追加新的生产线后,指定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆