|
-
Aug 13th, 2004, 07:26 PM
#1
Thread Starter
New Member
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.
-
Aug 13th, 2004, 07:50 PM
#2
Not sure if you need 12000 "fields" appended but if not just remove the for loop,
VB Code:
Dim sdata As String
sdata = ", FIELD1, FIELD2, FIELD3"
Dim i As Long
For i = 4 To 12000
sdata = sdata & ", FIELD" & i
Next i
Open "e:\file.txt" For Append As #1
Print #1, sdata
Close #1
-
Aug 13th, 2004, 08:00 PM
#3
Thread Starter
New Member
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
-
Aug 13th, 2004, 08:18 PM
#4
So, hmm, the data needs to literally be FIELD1, FIELD2, FIELD3, after each line?
No problemo!
VB Code:
Dim ReadBuffer As String, OutBuffer As String
Open "c:\source.txt" For Input As #1
'I have heard LOF(1) might give wrong results, thus FileLen
ReadBuffer = Input(FileLen("c:\source.txt"), 1)
Close #1
'replace linechange with the wanted string and a linechange
OutBuffer = Replace$(ReadBuffer, vbCrlLf, ", FIELD1, FIELD2, FIELD3" & vbCrLf)
Open "c:\output.txt" For Output As #1
Print #1, OutBuffer;
Close #1
Hope this is what you're looking for And I hope the lines are separeted with vbCrLf, not vbCr or vbLf...
-
Aug 13th, 2004, 08:34 PM
#5
This was my method,
VB Code:
Dim tdata As String
Dim sdata As String
Dim myarr() As String
Dim zdata As String
Dim i As Long
Dim rdata As String
On Error Resume Next
sdata = ", FIELD1, FIELD2, FIELD3"
Open "e:\input.txt" For Input As #1
For i = 0 To 120
Line Input #1, tdata
rdata = rdata & tdata & ";"
Next i
Close #1
Open "e:\output.txt" For Output As #1
myarr = Split(rdata, ";")
For i = LBound(myarr) To UBound(myarr)
zdata = myarr(i) & sdata
Print #1, zdata
Next i
Close #1
On Error GoTo 0
-
Aug 13th, 2004, 08:38 PM
#6
Thread Starter
New Member
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
-
Aug 13th, 2004, 08:51 PM
#7
Thread Starter
New Member
VB Code:
Private Sub cmdConcat_Click()
Dim StartDate, BoardName, BoardType, Concatenation As String
StartDate = txtStartDate.Text
BoardName = txtBoardName.Text
BoardType = txtBoardType.Text
Concatenation = "E4,,," + StartDate + "," + BoardName + ",1," + BoardType
Dim ReadBuffer As String, OutBuffer As String
Open "c:\PROM\MyMMSR.txt" For Input As #1
'I have heard LOF(1) might give wrong results, thus FileLen
ReadBuffer = Input(FileLen("c:\PROM\MyMMSR.txt"), 1)
Close #1
OutBuffer = Replace$(ReadBuffer, "E4", Concatenation)
Open "c:\PROM\MyMMSR.txt" For Output As #1
Print #1, OutBuffer;
Close #1
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
-
Aug 13th, 2004, 09:38 PM
#8
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.
-
Aug 13th, 2004, 09:53 PM
#9
Thread Starter
New Member
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.
-
Aug 13th, 2004, 10:00 PM
#10
-
Aug 13th, 2004, 10:03 PM
#11
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|