Deleting Last Carriage Return from Text File 
Author Message
 Deleting Last Carriage Return from Text File

Hi All,

I have a module which exports a load of text from my
tables and sticks them into a text file - I want the last
entry to end without a carriage return, but no matter what
I've tried I always end up with the unwanted carriage
return (which has adverse effects on what I do with the
text file afterwards). Anybody got any suggestions as to
how I could get the module to open the text file, go to
the end of the text and delete the last carriage return?

Thanks a lot!

Here is the part of the code that writes the records to
the file:
_______________________________________________________
Dim objEntry As TextFileEntry
Dim colEntries As New Collection

Dim strFileOut As String 'o/p file name
Dim intFile As Integer 'o/p file no.

'output the text file
    intFile = FreeFile
    Open strFileOut For Output As #intFile
'write the records
        For Each objEntry In colEntries
        Debug.Print objEntry.getAll
        Print #intFile, objEntry.getAll
    Next
    Close #FreeFile
_______________________________________________________



Tue, 03 Feb 2004 00:18:30 GMT  
 Deleting Last Carriage Return from Text File
Try rewriting your loop as

Dim blnIsLastRec as boolean
Dim i as integer
  for i=1 to colEntries.Count
    set objEntry = colEntries.item(i)
    Debug.Print objEntry.getAll
    blnIsLastRec = (i=colEntries.Count)
    Print #intFile, objEntry.getAll;
    if (blnIsLastRec) then
     ' do nothing
    Else
     Print #intFile,
    End if

 -- Dev

Quote:
>-----Original Message-----
>Hi All,

>I have a module which exports a load of text from my
>tables and sticks them into a text file - I want the last
>entry to end without a carriage return, but no matter
what
>I've tried I always end up with the unwanted carriage
>return (which has adverse effects on what I do with the
>text file afterwards). Anybody got any suggestions as to
>how I could get the module to open the text file, go to
>the end of the text and delete the last carriage return?

>Thanks a lot!

>Here is the part of the code that writes the records to
>the file:
>_______________________________________________________
>Dim objEntry As TextFileEntry
>Dim colEntries As New Collection

>Dim strFileOut As String 'o/p file name
>Dim intFile As Integer 'o/p file no.

>'output the text file
>    intFile = FreeFile
>    Open strFileOut For Output As #intFile
>'write the records
>        For Each objEntry In colEntries
>        Debug.Print objEntry.getAll
>        Print #intFile, objEntry.getAll
>    Next
>    Close #FreeFile
>_______________________________________________________
>.



Tue, 03 Feb 2004 01:02:03 GMT  
 Deleting Last Carriage Return from Text File

<<
<<Hi All,
<<
<<I have a module which exports a load of text from my
<<tables and sticks them into a text file - I want the last
<<entry to end without a carriage return, but no matter what
<<I've tried I always end up with the unwanted carriage
<<return (which has adverse effects on what I do with the
<<text file afterwards). Anybody got any suggestions as to
<<how I could get the module to open the text file, go to
<<the end of the text and delete the last carriage return?
<<

Someone else may have a better way, but here's one way you can do it. Essentially open the file for binary access
afterwards and change the last two bytes (carriage return and linefeed) to spaces. So you won't get an extra line at the
bottom, but you will have two extra spaces at the end of the last record.

   intFile = FreeFile
   strFileOut = "C:\Output.txt"
   Open strFileOut For Output As #intFile
   For Each objEntry In colEntries
       Debug.Print objEntry.getAll
       Print #intFile, objEntry.getAll
   Next
   Close #intFile

   intFile = FreeFile
   Open strFileOut For Binary As #intFile
   varFileSize = LOF(intFile)
  'Change the next to last byte from Chr(13) to a space
   Put intFile, varFileSize - 1, " "

  'Change the last byte from Chr(10) to a space

   Put intFile, varFileSize, " "
   Close #intFile

Hope this helps!

Sincerely,

Keith Fink
Microsoft Developer Support



Tue, 03 Feb 2004 04:31:21 GMT  
 Deleting Last Carriage Return from Text File
Dev & Keith,

Many thanks for your solutions - both work well. I'm going
with Dev's for the moment as it is slightly more efficient.

Thanks again - keep up the good work!

David



Tue, 03 Feb 2004 22:48:43 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. newbie: strip the carriage returns from a text file

2. Delete last line of a text file

3. Deleting the last charachter of a text file

4. Put a carriage return in text

5. Carriage Returns In Text Boxes

6. HELP - representing carriage return for a text field?

7. Carriage Return in text box

8. carriage return in a text box?

9. Saving Text without Return Carriage

10. Carriage return in text box?

11. How to hide linefeed, and carriage return characters in text boxes

12. Inserting a carriage return in a text box

 

 
Powered by phpBB® Forum Software