vb 脚本仅删除文本文件中的特定行 [英] vb script to remove only specific lines in a text file

查看:53
本文介绍了vb 脚本仅删除文本文件中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人愿意帮我写一个 vbscript 吗?我有一个简单的文本文件存储在 D:\Projects 上.

Would someone be kind enough to help me out with a vbscript? I have a simple text file stored on D:\Projects.

这是内容的一小部分示例(在记事本中完成):

Here's a small sample of the contents (done in Notepad):

Header 1 (this will always vary and be one simple line of text)

Captain:  John Doe 1
Team Roster: Temp\filename blah blah
Team Roster: Permanent\filename blah blah

Header 2 (this will always vary and be one simple line of text)

Captain:  John Doe 2
Team Roster: Temp\filename blah blah
Team Roster: Permanent\filename blah blah

我需要的是删除文件中除标题之外的所有内容,并且每个标题都需要重复.所以最终的结果是这样的:

What I need is for everything to be deleted in the file except for the header and each of the headers needs to be repeated. So the end result looks like this:

Header line 1
Header line 1
Header line 2
Header line 2  etc...

我意识到人们在空闲时间来到这里做志愿者,因此非常感谢任何帮助.我不知道如何编程,否则我会自己做.谢谢.

I realize people come here in their free time to volunteer so any help would be very much appreciated. I don't know how to program otherwise I would do this myself. Thanks.

推荐答案

假设文件中始终不存在Captain"和Team Roster",并且 Header 正在更改,您可以执行以下操作(输出将是.stripped.txt)

Assuming "Captain" and "Team Roster" are always excepted to exist in the file and the Header is changing, you could do something like this (output will be .stripped.txt)

stripFile "D:\Projects\File.txt"

Sub stripFile(fileName)

  Dim strOutputFile, strInputFile, strLine
  Dim objFSO, objOutputFile, objInputFile

  strOutputFile = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)) & fileName & ".stripped.txt"

  Set objFSO = CreateObject("Scripting.FileSystemObject") 

  Set objOutputFile = objFSO.OpenTextFile(strOutputFile, 8, True)   
  Set objInputFile = objFSO.OpenTextFile(fileName, 1)

  Do While objInputFile.AtEndOfStream = False
    strLine = objInputFile.ReadLine

    If Mid(strLine, 1, 7) <> "Captain" Then
      If Mid(strLine, 1, 11) <> "Team Roster" Then
        If strLine <> "" Then
          objOutputFile.WriteLine strLine
        End If
      End If
    End If
  Loop  

  objOutputFile.Close
  objInputFile.Close

  Set objOutputFile = Nothing
  Set objInputFile = Nothing
  Set objFSO = Nothing

End Sub

这篇关于vb 脚本仅删除文本文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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