在特定文本后读取并在到达空行时停止读取,然后将其写入输出文件

时间:2014-08-14 06:48:05

标签: text vbscript

目前我只能读取和写入从输入文件到输出文件的所有内容。但是我只想读取book1 | book2 | book3之后的行,并在它到达空行时停止读取,然后根据读取的内容写入输出文件。我找到了这段代码nStr(strLine, sStringToFind),但我不知道它是否在这里正常工作,或者如何使用它。

我的输入文件 enter image description here

我的当前输出基于我的代码 enter image description here

这是我想要实现的输出 enter image description here

 Set objWorkbook = objFSO2.OpenTextFile(inputFilePath)

              Dim strLine
              Dim intLineCounter

              intLineCounter = 0    

              Do Until objWorkbook.AtEndOfStream

              strLine = objWorkbook.ReadLine

              objFile.WriteLine(strLine) 

              intLineCounter = intLineCounter + 1
              Loop

3 个答案:

答案 0 :(得分:2)

将输入文件的一部分复制到输出文件 - 顶级结构:

  1. 制剂
  2. 读/写循环
  3. 清理
  4. 准备工作:打开文件和设置状态变量以表示写作

    1. 设置tsIn = oFS.OpenTextFile(...)
    2. 设置tsOut = oFS.CreateTextFile(...)
    3. bWrite = False
    4. CleanUp:关闭文件

      1. tsOut.Close
      2. tsIn.Close
      3. 读/写循环

        Do Until tsIn.AtEndOfStream
           sLine = tsIn.ReadLine()
           ... Kernel ...
        Loop
        

        有趣的事件

        1. sLine =“book1 | book2 | book3”==>
        2. 从下一行写
        3. sLine =“” 事件后1 ==>完成
        4. 内核:

          If sLine = "book1|book2|book3" Then
             bWrite = True
          Else
             If bWrite Then
                If sLine = "" Then
                   Exit Do
                Else
                   tsOut.WriteLine sLine
                End If
             End If
          End If
          

          更新wrt @ Hackoo的回答:

          体面实施上述计划:

          Option Explicit
          
          Dim oFS    : Set oFS   = CreateObject("Scripting.FileSystemObject")
          Dim tsIn   : Set tsIn  = oFS.OpenTextFile("..\data\25301708.txt")
          Dim tsOut  : Set tsOut = oFS.CreateTextFile("..\data\25301708-out.txt")
          Dim bWrite : bWrite    = False
          Dim sLine
          
          Do Until tsIn.AtEndOfStream
             sLine = tsIn.ReadLine()
             If sLine = "book1|book2|book3" Then
                bWrite = True
             Else
                If bWrite Then
                   If sLine = "" Then
                      Exit Do
                   Else
                      tsOut.WriteLine sLine
                   End If
                End If
             End If
          Loop
          
          tsOut.Close
          tsIn.Close
          

答案 1 :(得分:2)

试试这段代码:

Option Explicit
Dim Titre,Data,s,LogFile,ws,fso,Result,Lines,Line
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("Wscript.Shell")
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "txt"
if fso.FileExists(LogFile) Then 
    fso.DeleteFile LogFile
end If
Data = ReadFileText("file.txt")
Result = Between_Ands(Data,"book1|book2|book3","fax1|fax2|fax3")
Lines = Split(Result,vbCrLf)
For Each Line In Lines
    If Len(Line) > 0 Then
        WriteLog Line,LogFile
    end if
Next
ws.Run LogFile,1,False
'**********************************************************************************************
Function Between_Ands(ByVal Full_String, ByVal First_Delimiter, ByVal Second_Delimiter)
    Dim Pos,Pos2
    Pos = InStr(Full_String, First_Delimiter)
    Pos2 = InStr(Full_String, Second_Delimiter)
    If Pos = 0 Or Pos2 = 0 Then
        Between_Ands = "Missing Delimiter"
        Exit Function
    End If
    Between_Ands = Mid(Full_String, Pos + Len(First_Delimiter), Pos2 - (Pos + Len(First_Delimiter)))
End Function
'***********************************************************************************************
Function ReadFileText(sFile)
    Dim objFSO,oTS,sText
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FileExists(sFile) Then
        MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) & " dosen't exists !",VbCritical,"CRITICAL ERROR"
        Wscript.Quit
    Else
        Set oTS = objFSO.OpenTextFile(sFile)
        sText = oTS.ReadAll
        oTS.close
        set oTS = nothing
        Set objFSO = nothing
        ReadFileText = sText
    End if
End Function 
'***********************************************************************************************
Sub WriteLog(strText,LogFile)
    Dim fs,ts 
    Const ForAppending = 8
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
    ts.WriteLine strText
    ts.Close
End Sub
'************************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************

使用 Ekkehard.Horner

提出的解决方案

你必须这样做:

Option Explicit
Dim Titre,Data,s,LogFile,ws,fso,Result,Lines,Line,bWrite
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("Wscript.Shell")
bWrite = False
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "txt"
if fso.FileExists(LogFile) Then 
    fso.DeleteFile LogFile
end If
Data = ReadFileText("file.txt")
Lines = Split(Data,vbCrLf)
For Each Line In Lines
    If Line = "book1|book2|book3" Then
        bWrite = True
    Else
        If bWrite Then
            If Line = "" Then
                Exit For
            Else
                WriteLog Line,LogFile
            End If
        End if
    End if
Next
ws.Run LogFile,1,False
'***********************************************************************************************
    Function ReadFileText(sFile)
        Dim objFSO,oTS,sText
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        If Not objFSO.FileExists(sFile) Then
            MsgBox "CRITICAL ERROR " & VbCrLF & "The File "& DblQuote(sFile) & " dosen't exists !",VbCritical,"CRITICAL ERROR"
            Wscript.Quit
        Else
            Set oTS = objFSO.OpenTextFile(sFile)
            sText = oTS.ReadAll
            oTS.close
            set oTS = nothing
            Set objFSO = nothing
            ReadFileText = sText
        End if
    End Function 
'***********************************************************************************************
    Sub WriteLog(strText,LogFile)
        Dim fs,ts 
        Const ForAppending = 8
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
        ts.WriteLine strText
        ts.Close
    End Sub
'************************************************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
'**********************************************************************************************

答案 2 :(得分:0)

今晚我发现自己需要类似的脚本。 Instr只是在搜索字符串时就像布尔值一样,即使从技术上讲它会给您匹配的位置(在您要搜索的另一个字符串中)。我发现它也比使用equals进行直接比较更具灵活性。 Else If巩固了主要逻辑:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

strInFile = "inFile.txt" 'Put a full path if you want
strOutFile = "outFile.txt" 

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFS.OpenTextFile(strInFile, ForReading)
Set objOutFile = objFS.OpenTextFile(strOutFile, ForWriting, True)
writeNow = false

Do Until objInFile.AtEndOfStream
   sReadLine = objInFile.ReadLine()

    If instr(sReadLine, "book1|book2|book3") Then
        writeNow = true                'We exit the if statement here ready to write on the next loop
    ElseIf (writeNow AND Trim(sReadLine) = "") Then
        Exit Do                        'We exit the loop, finished writing
    ElseIf writeNow Then
        objOutFile.WriteLine sReadLine 'Otherwise, we write
    End If
Loop

objOutFile.Close
objInFile.Close

VBScript万岁!