Results 1 to 10 of 10

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

  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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,958

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

    You've posted this in the VB.NET forum. Are you looking for a VB.NET solution or a VBScript solution?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    5

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

    My apologies. I am new in the forum. I am looking for VBScript solution.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,958

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

    Then you probably ought to have posted in the forum with VBScript in the title, rather than the first one on the list. New to this forum but not new to the web, I would think. I've asked the mods to move this thread to the appropriate forum.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    5

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

    Do I have to move the post to a different forum? If so, do you know the URL for that? Thanks

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,958

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

    Quote Originally Posted by seekingHelp View Post
    Do I have to move the post to a different forum? If so, do you know the URL for that? Thanks
    You can't move it yourself. I have used the Report Post icon under your first post to send a message to the mods, asking them to do it. You can do the same in future if you ever need to bring their attention to a specific post for whatever reason.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    5

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

    Thank you and sorry for all the trouble.

  8. #8
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,034

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

    It's nearly impossible to mentally step through unformatted, non-indented code like was posted, but there is no way that your "Exit Do" and "Exit For" lines of code are appropriate. Step 1 - get rid of them and see what happens.

    There may be one or more issues beyond that, but that's all I can offer. Good luck.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,430

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

    Welcome to VBForums

    I have moved this thread from the 'VB.Net' forum to the 'VBScript' forum

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2020
    Posts
    5

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

    Can anyone help? Thanks in advance.

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