Removing vbCrlf's from a text file 
Author Message
 Removing vbCrlf's from a text file

We are receiving files from a mainframe app. The app puts a carriage
return line feed at the end of every line. Is there a way to process this
file a only take out all but the last carriage return line feed.




Sat, 31 Aug 2002 03:00:00 GMT  
 Removing vbCrlf's from a text file
Yes.

See this code

'//=================================================
'// CLASS       : CCopyFile
'// DESCRIPTION : -
'// AUTHOR      : Harvey T.
'// LAST REVIEW : -
'//=================================================
Option Explicit

Public Event Percent(Value As Single)

Private Const nBLOB As Long = 2 ^ 10

Public Function CopyFileByBlobs( _
       SourceFile As String, _
       NewFileName As String, _
       Optional EventPercent As Boolean = False _
    ) As Boolean

    Dim ReadChannel     As Long
    Dim WriteChannel    As Long
    Dim SourceBytes     As Long
    Dim Blobs           As Long
    Dim BlobX           As Long
    Dim Buffer          As String
    Dim CopyCount       As Long
    Dim PercentRelation As Single
    Dim i               As Long

    On Error GoTo ErrHandler

    ReadChannel = FreeFile
    Open SourceFile For Binary Access Read As #ReadChannel
    WriteChannel = FreeFile
    Open NewFileName For Binary Access Write As #WriteChannel

    SourceBytes = LOF(ReadChannel)
    Blobs = Int((SourceBytes / nBLOB))
    BlobX = (SourceBytes - Blobs * nBLOB)
    PercentRelation = 100 / SourceBytes

    If Blobs > 0 Then
       For i = 1 To Blobs
           Buffer = Input$(nBLOB, #ReadChannel)
           Put #WriteChannel, , Buffer
           If EventPercent Then
              CopyCount = CopyCount + nBLOB
              RaiseEvent Percent(PercentRelation * CopyCount)
           End If
       Next
    End If
    If BlobX > 0 Then
       Buffer = Input$(BlobX, #ReadChannel)
       Put #WriteChannel, , Buffer
       If EventPercent Then
          CopyCount = CopyCount + BlobX
          RaiseEvent Percent(PercentRelation * CopyCount)
       End If
    End If
    Close #ReadChannel, #WriteChannel
    RaiseEvent Percent(0)

    CopyFileByBlobs = True
    Exit Function

ErrHandler:
    CopyFileByBlobs = False
End Function

--
after each:
           Buffer = Input$(nBLOB, #ReadChannel)
add the line:
           Buffer = Replace(Buffer, vbCrLf, vbNullString)

So, the vbCrLf is removed.
(NOTE. This procedure is able to show a progress bar... if you like)
________________________________________________
Harvey Triana
Visual Basic Developer MVP
http://www.eidos.es/VeXPERT


________________________________________________


We are receiving files from a mainframe app. The app puts a carriage
return line feed at the end of every line. Is there a way to process this
file a only take out all but the last carriage return line feed.




Sat, 31 Aug 2002 03:00:00 GMT  
 Removing vbCrlf's from a text file
Alan,

Read the file in using the LineInput command.
Then print the read line to an output file.
When you print the last line, just add a cr/lf to it.

Dim Incoming as String
Open "Mainframe.txt" for input as #1
Open "Cleaned.txt" for output as #2
Do until EOF(1)
  LineInput #1, Incoming
  Print #2, Incoming;
Loop
Close #1
Print #2, vbCrLf
Close #2

Hope this helps you out.

-Shane Russell-


Quote:
> We are receiving files from a mainframe app. The app puts
a carriage
> return line feed at the end of every line. Is there a way
to process this
> file a only take out all but the last carriage return line
feed.





Sat, 31 Aug 2002 03:00:00 GMT  
 Removing vbCrlf's from a text file
Will that really work? It would seem that if the lines from the mainframe
end with a LineFeed or a LineFeed-CarriageReturn sequence (reversed from
VB's sequence), these would be retained. Hence, reading in what you think is
one line might actually load in the entire file as a single line (since
there is no VB newline sequence to break it).

If would seem you would have to search the line (file?) just read in and
first search for vbLf & vbCr (the reversed sequence) replacing it with
vbNewLine. If that reversed sequence is not found, then search for all
solitary vbLf and replace them with vbNewLine. If that fails, then search
for all solitary vbCr and replace those with vbNewLine. The only problem
might be if the file is too large to fit in a string variable -- then the
file would have to be loaded in chucks (taking care not to split in the
middle of a vbLf & vbCr).

Rick


Quote:
> Alan,

> Read the file in using the LineInput command.
> Then print the read line to an output file.
> When you print the last line, just add a cr/lf to it.

> Dim Incoming as String
> Open "Mainframe.txt" for input as #1
> Open "Cleaned.txt" for output as #2
> Do until EOF(1)
>   LineInput #1, Incoming
>   Print #2, Incoming;
> Loop
> Close #1
> Print #2, vbCrLf
> Close #2

> Hope this helps you out.

> -Shane Russell-



> > We are receiving files from a mainframe app. The app puts
> a carriage
> > return line feed at the end of every line. Is there a way
> to process this
> > file a only take out all but the last carriage return line
> feed.





Sat, 31 Aug 2002 03:00:00 GMT  
 Removing vbCrlf's from a text file
Rick,
You're right if the lines end in anything except CR/FL,
but his question says that the lines end with CR/LF.
LineInput reads until it hits a CR/LF and leaves it off.

-Shane Russell-


Quote:
> Will that really work? It would seem that if the lines
from the mainframe
> end with a LineFeed or a LineFeed-CarriageReturn sequence
(reversed from
> VB's sequence), these would be retained. Hence, reading in
what you think is
> one line might actually load in the entire file as a
single line (since
> there is no VB newline sequence to break it).

> If would seem you would have to search the line (file?)
just read in and
> first search for vbLf & vbCr (the reversed sequence)
replacing it with
> vbNewLine. If that reversed sequence is not found, then
search for all
> solitary vbLf and replace them with vbNewLine. If that
fails, then search
> for all solitary vbCr and replace those with vbNewLine.
The only problem
> might be if the file is too large to fit in a string

variable -- then the

- Show quoted text -

Quote:
> file would have to be loaded in chucks (taking care not to
split in the
> middle of a vbLf & vbCr).

> Rick



Sat, 31 Aug 2002 03:00:00 GMT  
 Removing vbCrlf's from a text file
Good point -- I read mainframe and thought Unix (but then generalize to any
non-DOS/Windows operating system). One point about your post though . . .
you used

     Print #2, Incoming;

to print the line minus the NewLine sequence. That will {*filter*}up the joined
lines last-character-to-first-character. Wouldn't

     Print #2, Incoming & " ";

be more readable?

Rick


Quote:
> Rick,
> You're right if the lines end in anything except CR/FL,
> but his question says that the lines end with CR/LF.
> LineInput reads until it hits a CR/LF and leaves it off.

> -Shane Russell-



> > Will that really work? It would seem that if the lines
> from the mainframe
> > end with a LineFeed or a LineFeed-CarriageReturn sequence
> (reversed from
> > VB's sequence), these would be retained. Hence, reading in
> what you think is
> > one line might actually load in the entire file as a
> single line (since
> > there is no VB newline sequence to break it).

> > If would seem you would have to search the line (file?)
> just read in and
> > first search for vbLf & vbCr (the reversed sequence)
> replacing it with
> > vbNewLine. If that reversed sequence is not found, then
> search for all
> > solitary vbLf and replace them with vbNewLine. If that
> fails, then search
> > for all solitary vbCr and replace those with vbNewLine.
> The only problem
> > might be if the file is too large to fit in a string
> variable -- then the
> > file would have to be loaded in chucks (taking care not to
> split in the
> > middle of a vbLf & vbCr).

> > Rick



Sat, 31 Aug 2002 03:00:00 GMT  
 Removing vbCrlf's from a text file
Yep,

-Shane-


Quote:
> Good point -- I read mainframe and thought Unix (but then
generalize to any
> non-DOS/Windows operating system). One point about your
post though . . .
> you used

>      Print #2, Incoming;

> to print the line minus the NewLine sequence. That will
{*filter*}up the joined
> lines last-character-to-first-character. Wouldn't

>      Print #2, Incoming & " ";

> be more readable?

> Rick


message

> > Rick,
> > You're right if the lines end in anything except CR/FL,
> > but his question says that the lines end with CR/LF.
> > LineInput reads until it hits a CR/LF and leaves it off.

> > -Shane Russell-


message

> > > Will that really work? It would seem that if the lines
> > from the mainframe
> > > end with a LineFeed or a LineFeed-CarriageReturn
sequence
> > (reversed from
> > > VB's sequence), these would be retained. Hence,
reading in
> > what you think is
> > > one line might actually load in the entire file as a
> > single line (since
> > > there is no VB newline sequence to break it).

> > > If would seem you would have to search the line
(file?)
> > just read in and
> > > first search for vbLf & vbCr (the reversed sequence)
> > replacing it with
> > > vbNewLine. If that reversed sequence is not found,
then
> > search for all
> > > solitary vbLf and replace them with vbNewLine. If that
> > fails, then search
> > > for all solitary vbCr and replace those with
vbNewLine.
> > The only problem
> > > might be if the file is too large to fit in a string
> > variable -- then the
> > > file would have to be loaded in chucks (taking care
not to
> > split in the
> > > middle of a vbLf & vbCr).

> > > Rick



Sat, 31 Aug 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Removing text from a text file

2. vbCrLf after attachment - (not '.Body')

3. vbcrlf or vbCrLf

4. Rich Text Box: Can I remove text and keep multicolored text

5. What's the binary code for VbCrLf

6. Question about removing character from text file

7. Removing spaces when running module to text file

8. Remove control character from text file using qbasic

9. Script to remove text from a file

10. Text File - checking and removing lines

11. Reading a text file and removing lines???

12. removing page returns from a text file, help!

 

 
Powered by phpBB® Forum Software