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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File Line Manipulation 1

Status
Not open for further replies.

jcw5107

Technical User
Jan 31, 2007
66
US
I have a text file that has consistent formatting, except for one thing. The last column(field) in each line is basically a "comments" type field. But instead of the "comments" text staying on the same line, it continues on the next line starting at the last column location. I need to be able to get each continuation line appended or reposistioned back on to the end of the current line..? I sure hope that makes sense..!! Each line has a "Task#" column(field) that starts at column 4, and the "comments" column starts at column 68, it the current line has comments that continue to the next line, the line will be blank all the way to column 68, where the continuation starts. If no continuation, then the next line will have a "Task#" starting at column 4..
Text file example:
E 131360 EO B767-2510-8111 A 25 N 3 LO DEACTIVATE FLT DECK OVRHEAD STOWAGE COMPARTMENT DUE OPENING
ON LANDING. INSTALL TAPE TO PREVENT OPENING AND APPLY "DO
NOT USE" PLACARD. THIS EO SUPERCEDES B767-2510-8086 (OVERHD
STOWAGE PLACARD)
E 138944 EO B767-2510-8278 B 25 Y 3 LO MODIFY OVERHEAD STOWAGE COMPARTMENT DOOR LATCHS (WARRANTY)

Which I need for it to read like:
E 131360 EO B767-2510-8111 A 25 N 3 LO DEACTIVATE FLT DECK OVRHEAD STOWAGE COMPARTMENT DUE OPENING ON LANDING. INSTALL TAPE TO PREVENT OPENING AND APPLY "DO NOT USE" PLACARD. THIS EO SUPERCEDES B767-2510-8086 (OVERHD STOWAGE PLACARD)
E 138944 EO B767-2510-8278 B 25 Y 3 LO MODIFY OVERHEAD STOWAGE COMPARTMENT DOOR LATCHS (WARRANTY)

Any suggestion or examples...??
Thanks in advance..!!
jcw5107
 
jcw5107,
Here is a rough concept you could use to 'normalize' the text file prior to processing the file. This routine takes a file in the original format you provided ([tt]C:\FileIn.txt[/tt]) and produces a normalized file ([tt]C:\FileOut.txt[/tt]) in the format your looking for.
Code:
Sub NormalizeFile()
Dim intFileIn As Integer, intFileOut As Integer
Dim strReadBuffer As String, strWriteBuffer
intFileIn = FreeFile
Open "C:\FileIn.txt" For Input As #intFileIn
intFileOut = FreeFile
Open "C:\FileOut.txt" For Output As #intFileOut

'Read the first line before starting the loop
'so the first record is processed correctly
Line Input #intFileIn, strReadBuffer

'Loop through the rest of the file
Do
  'Append the read buffer to the write buffer
  strWriteBuffer = strWriteBuffer & " " & Trim(strReadBuffer)
  'Read the next line to see if new record
  Line Input #intFileIn, strReadBuffer
  'Test for a new record
  If Left(strReadBuffer, 1) <> " " Then
    'It is a new record so write the buffer and clear
    Print #intFileOut, strWriteBuffer
    strWriteBuffer = ""
  End If
Loop Until EOF(intFileIn)
Reset
End Sub

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
CautionMP,

YES.!!! Absolutely amazing...!! Star for you..!!
I got this to work in my application. One question (at least I hope)
On the part that is:
strWriteBuffer = strWriteBuffer & " " & Trim(strReadBuffer)
is there a way to "trim" all the spaces on the strReadBuffer section so its not appended way on the other end of the line...? It doing what it supposed just leaving big gaps in the "comments" column of the line.
Thanks for the help and fast response..!!
jcw5107
 
jcw5107,
I'm not aware of a built in function that will remove duplicate spaces from the middle of a string. You could use something like this:
Code:
While Len(strReadBuffer) <> Len(Replace(strReadBuffer, "  ", " "))
  strReadBuffer = Replace(strReadBuffer, "  ", " ")
Wend
I think you could incorporate like:
Code:
...
  If Left(strReadBuffer, 1) <> " " Then
    'It is a new record so write the buffer and clear
    Print #intFileOut, strWriteBuffer
    strWriteBuffer = ""
  Else
    While Len(strReadBuffer) <> Len(Replace(strReadBuffer, "  ", " "))
      strReadBuffer = Replace(strReadBuffer, "  ", " ")
    Wend
  End If
...
Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top