Results 1 to 11 of 11

Thread: Reading A Text File [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    11

    Question Reading A Text File [RESOLVED]

    Hello gents,
    I mainly program in java, in web apps using Dreamweaver and JDeveloper, however I need to put my rusty vb skills back in action. I have comma delimated text file, that looks like this.

    SSN, LAST_NAME, MOS, RANK

    I need to format this comma delimated text file to look like this

    SSN, LAST_NAME, MOS, RANK, FIELD1, FIELD2, FIELD3

    This text file contains over 12,000 records, and I know how this can be done easily with notepad and concatenating the FIELDS onto the end. However this Web application that I am developing requires that this text file be updated More than once a week. The file is used to insert into an Oracle 9i DB. The Field 1,2,3 are fields that are retrieved from a .jsp web page and are filled in for all the 12,000 records in the text file before it is loaded into a database. It would be nice if this was done using a single .exe built in VB. If I can get the file formatted correctly then I can take the inserting into the DB easily. I have seen on the web a lot about FileSystemObjects however I dont know how to use that class in VB. My copy of vb is less than legal, and because of this my MSDN is broken. Any help is apreciated.
    Last edited by IM_MARINE; Aug 13th, 2004 at 10:54 PM.

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    Not sure if you need 12000 "fields" appended but if not just remove the for loop,

    VB Code:
    1. Dim sdata As String
    2. sdata = ", FIELD1, FIELD2, FIELD3"
    3. Dim i As Long
    4. For i = 4 To 12000
    5. sdata = sdata & ", FIELD" & i
    6. Next i
    7. Open "e:\file.txt" For Append As #1
    8. Print #1, sdata
    9. Close #1

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    11

    TY

    Awesome man,
    Your reply has not yet solved my problem but it has pointed me in the right direction. I would post the file up here, but it obviously has confindential ssn's.

    SSN, LAST_NAME, MOS, GRADE
    SSN, LAST_NAME, MOS, GRADE
    SSN, LAST_NAME, MOS, GRADE
    SSN, LAST_NAME, MOS, GRADE

    Needs to be

    SSN, LAST_NAME, MOS, GRADE, FIELD1, FIELD2, FIELD3,
    SSN, LAST_NAME, MOS, GRADE, FIELD1, FIELD2, FIELD3,
    SSN, LAST_NAME, MOS, GRADE, FIELD1, FIELD2, FIELD3,
    SSN, LAST_NAME, MOS, GRADE, FIELD1, FIELD2, FIELD3,

    Your solution was

    SSN, LAST_NAME, MOS, GRADE
    SSN, LAST_NAME, MOS, GRADE
    SSN, LAST_NAME, MOS, GRADE
    SSN, LAST_NAME, MOS, GRADE, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3, FIELD1, FIELD2, FIELD3,




    See my dilema, lol, but thanks for the quick reply

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    So, hmm, the data needs to literally be FIELD1, FIELD2, FIELD3, after each line?

    No problemo!

    VB Code:
    1. Dim ReadBuffer As String, OutBuffer As String
    2.     Open "c:\source.txt" For Input As #1
    3.         'I have heard LOF(1) might give wrong results, thus FileLen
    4.         ReadBuffer = Input(FileLen("c:\source.txt"), 1)
    5.     Close #1
    6.     'replace linechange with the wanted string and a linechange
    7.     OutBuffer = Replace$(ReadBuffer, vbCrlLf, ", FIELD1, FIELD2, FIELD3" & vbCrLf)
    8.     Open "c:\output.txt" For Output As #1
    9.         Print #1, OutBuffer;
    10.     Close #1

    Hope this is what you're looking for And I hope the lines are separeted with vbCrLf, not vbCr or vbLf...

  5. #5
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    This was my method,

    VB Code:
    1. Dim tdata   As String
    2. Dim sdata   As String
    3. Dim myarr() As String
    4. Dim zdata   As String
    5. Dim i       As Long
    6. Dim rdata   As String
    7.     On Error Resume Next
    8.     sdata = ", FIELD1, FIELD2, FIELD3"
    9.     Open "e:\input.txt" For Input As #1
    10.     For i = 0 To 120
    11.         Line Input #1, tdata
    12.         rdata = rdata & tdata & ";"
    13.     Next i
    14.     Close #1
    15.    
    16.     Open "e:\output.txt" For Output As #1
    17.     myarr = Split(rdata, ";")
    18.     For i = LBound(myarr) To UBound(myarr)
    19.         zdata = myarr(i) & sdata
    20.         Print #1, zdata
    21.     Next i
    22.     Close #1
    23.     On Error GoTo 0

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    11

    Thanks a bunch

    Well both of your inputs were essential

    I'll post the solution with a dummy file in a minute, so you can see what I was going for.

    You guys are the backbone of the corps

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    11
    VB Code:
    1. Private Sub cmdConcat_Click()
    2.     Dim StartDate, BoardName, BoardType, Concatenation As String
    3.     StartDate = txtStartDate.Text
    4.     BoardName = txtBoardName.Text
    5.     BoardType = txtBoardType.Text
    6.     Concatenation = "E4,,," + StartDate + "," + BoardName + ",1," + BoardType
    7.  
    8. Dim ReadBuffer As String, OutBuffer As String
    9.     Open "c:\PROM\MyMMSR.txt" For Input As #1
    10.         'I have heard LOF(1) might give wrong results, thus FileLen
    11.         ReadBuffer = Input(FileLen("c:\PROM\MyMMSR.txt"), 1)
    12.     Close #1
    13.     OutBuffer = Replace$(ReadBuffer, "E4", Concatenation)
    14.     Open "c:\PROM\MyMMSR.txt" For Output As #1
    15.         Print #1, OutBuffer;
    16.     Close #1
    17.    
    18. End Sub



    Here is a screen shot of the app.
    and link http://www.griffzilla.com/images/VB_Example.bmp

    Here is what the file was
    123456789,CHOIKE,0402,E4
    123456789,HASKELL,0302,E4
    123456789,BLYTH,0302,E4
    123456789,BRANIGAN,0302,E4
    123456789,BRENNAN,0802,E4

    And here is the final output
    123456789,CHOIKE,0402,E4,,,Aug 20, 2004,Cpl FY 2004,1,Active Duty
    123456789,HASKELL,0302,E4,,,Aug 20, 2004,Cpl FY 2004,1,Active Duty
    123456789,BLYTH,0302,E4,,,Aug 20, 2004,Cpl FY 2004,1,Active Duty
    123456789,BRANIGAN,0302,E4,,,Aug 20, 2004,Cpl FY 2004,1,Active Duty
    123456789,BRENNAN,0802,E4,,,Aug 20, 2004,Cpl FY 2004,1,Active Duty

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    if it is correct, then edit the original post to add [RESOLVED] to the subject to close the thread.
    Last edited by dglienna; Aug 13th, 2004 at 09:43 PM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    11
    Yes as you can see, the three fields are the same for all the records in the flat file. They are obtained from the controls. There is only one text file involved, and I just needed a simple way for the user to "reformat" it so it would have all the values needed to insert into the database with a one shot, one kill mentality.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    then I guess it is done.

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    11
    nearly, thank you all for the help...

    however there are more pieces to the puzzle.

    as you can read in my new topic...lol

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