且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

vb 脚本仅删除文本文件中的特定行

更新时间:2023-12-05 20:53:22

假设文件中始终不存在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