Results 1 to 10 of 10

Thread: Searching and Reading flat file lines until certain line string

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    5

    Searching and Reading flat file lines until certain line string

    Hi guys,
    I can go no where with this problem. I have an "Account.txt" that contains all the account numbers I need to search. I also have an "Input.txt" that has all customer account transaction data. What I need to do is, read the account.txt, put all the account number in an array. Then if any matched account number in the Input.txt, read the account number and its transaction data lines to Output.txt. After that, check for next account number, if not matched, do not read, if matched, do the same, until the end of file.

    Here is the account.txt format and data:
    \A0000000000002
    \A0000000000004
    \A0000000000005

    Here is Input.txt file data:
    Code:
    This is a Header Line Check
    \A0000000000001   Testing
    Data Line T12
    Data Line T12
    \A0000000000002  Testing
    Data Line T21
    Data Line T22
    Data Line T23
    Data Line T24
    Data Line T25
    Data Line T26
    Data Line T27
    \A0000000000003  Testing
    Data Line T31
    Data Line T32
    \A0000000000004     Testing
    Data Line T11
    \A0000000000005    Testing
    Data Line T51
    Data Line T52
    \A0000000000006    Testing
    Data Line T61
    Data Line T62
    Output.txt I expect to have
    Code:
    This is a Header Line Check
    \A0000000000002  Testing
    Data Line T21
    Data Line T22
    Data Line T23
    Data Line T24
    Data Line T25
    Data Line T26
    Data Line T27
    \A0000000000004     Testing
    Data Line T11
    \A0000000000005    Testing
    Data Line T51
    Data Line T52
    Here is the code I have:
    Code:
    Dim strLine, account
    Dim searchStr, CheckStr, CheckStr1
    Dim Flag
    Flag = 0
     
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objOpen = objFSO.OpenTextFile("C:\Accounts.txt", ForReading)
    Dim arrFileName()
     
    i = 0
    Do While objOpen.AtEndOfStream <> true
        text = objOpen.ReadLine
        'WSCript.Echo text
            ReDim Preserve arrFileName(i)
        arrFileName(i) = text
        i=i+1
    loop
    objOpen.Close
    Set objOpen = Nothing
    Set objFSO = Nothing
     
    sFile = "C:\Input.txt"
    dFile = "C:\Output.txt"
     
    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(sFile,1)
    Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(dFile,2,true)
     
    do Until objFileToRead.AtEndOfStream
            strLine = objFileToRead.ReadLine()
            Account = Mid(strLine, 1, 15)
            searchStr = Mid(strLine, 1, 2)
     
    If InStr(strLine,"This is a Header Line") > 0 Then
       WScript.Echo strLine
       'objFileToWrite.WriteLine(strLine)
    End If
    For i = 0 To UBound(arrFileName)
       If arrFileName(i) = Account Then
          Flag = 1
          WScript.Echo strLine
         'objFileToWrite.WriteLine(strLine)
         i=i+1
     
    ElseIf(Flag = 1 AND searchStr = "\A") Then
    WScript.Echo strLine
    Exit Do
     
    ElseIf Flag = 1 Then
    WScript.Echo strLine
    'objFileToWrite.WriteLine(strLine)
     
    End If
    Exit For
    Next
     
    Loop
     
    objFileToWrite.Close
    Set objFileToWrite = Nothing
    objFileToRead.Close
    Set objFileToRead = Nothing
    By the way, it is in VBS. Currently, after I print out the first matched account data, it loop out. Please help and thanks in advance.
    SeekingHelp
    Last edited by si_the_geek; Mar 22nd, 2020 at 11:01 AM. Reason: added Code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width