Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File Manipulation/Restructure 1

Status
Not open for further replies.

jcw5107

Technical User
Jan 31, 2007
66
0
0
US
Hey now...!!
I have a text file that is formatted like this:
00 120 SDF
1)Fuel pump inop
2)#2 Hyd pump leak
30 250 ONT
1)#2 Main wheel cut

I'm tryin' re-format the file to look like this:
00 120 SDF 1)Fuel pump inop
00 120 SDF 2)#2 Hyd pump leak
30 250 ONT 1)#2 Main wheel cut

For each line that starts with 00 or 10 or 20 or 30 - for each line underneath that, append it at the end of previous line, and repeat it till the next line with 00 or 10 or 20 or 30...
I hope this makes sense...!!

Any suggestions or examples...??
Thanks in advance..!!
jcw5107
 

Open the file for Input and a new file for output.
Read the first line to a text variable1
Until the end file
Read the next line to a text variable2
If first character of variable2 is space
write to output file -->variable1 + variable2
Else
assign variable1 = variable2
End If
End loop
Close the files.

 
JerryKlmns,

Something like this....

Sub test()
Dim intFileIn As Integer, intFileOut As Integer
Dim strReadBuffer As String, strWriteBuffer

intFileIn = FreeFile
Open "D:\UPSDATA\OOSReport.txt" For Input As #1
intFileOut = FreeFile
Open "D:\UPSDATA\OOSReport1.txt" For Output As #2

Do While Not EOF(intFileIn)
Line Input #1, strReadBuffer
strWriteBuffer = strReadBuffer
If Left(strReadBuffer, 3) = "00 " _
Or Left(strReadBuffer, 3) = "10 " _
Or Left(strReadBuffer, 3) = "20 " Then
Line Input #intFileIn, strReadBuffer
strWriteBuffer = strWriteBuffer & " " & strReadBuffer
End If
Print #2, strWriteBuffer
Loop
Close #1
Close #2

End Sub

This almost works... Whenever there is more then 1 line underneath a line that starts with 10 or 20 or 30 - it only appends the 1st line at the end of the "read line".
I need for it to append every line under the "read line" at the end of the "read line" and repeat the "read line" for each occurance and then go to the next "read line".

Any more suggestions..???
Thanks for the help..!!!
jcw5107
 
Reread carefully JerryKlmns's suggestion.
 
Some amendments..

Code:
Sub test()
Dim intFileIn As Long, intFileOut As Long
Dim strReadBuffer1 As String, strReadBuffer2 As String

intFileIn = FreeFile
Open "D:\UPSDATA\OOSReport.txt" For Input Access Read Lock Read Write As #intFileIn 
intFileOut = FreeFile
Open "D:\UPSDATA\OOSReport1.txt" For Output Access Write Lock Read Write As #intFileOut 

Line Input #intFileIn, strReadBuffer1 
Do While Not EOF(intFileIn)
   Line Input #intFileIn, strReadBuffer2 
   If Left(strReadBuffer2, 1) = " " Then
      Print #intFileOut, strReadBuffer1 & " " & strReadBuffer2 
   Else   
      strReadBuffer1 = strReadBuffer2 
   End If
Loop
Close #intFileIn
Close #intFileOut

End Sub
 
JerryKlmns,

That did the trick..!!
Thank you...!!!

Star for ya..!!!

Thanks..!!
jcw5107
 
jcw5107

Glad to be of help and happy for seeing you doing the effort to code the logical steps suggested.
 
JerryKlmns,

Hey thanks..!!
I have learned so much from you guys over the past few years..!! Absolutely amazing what I'm doin' with data where I work. We have been able to view our data in ways have never thought due to the things I have learned from Tek-Tips..

Awesome..!!!!

jcw5107
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top