Removing last two characters in a line

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi all

I've got a file (CSV) that has extra characters in it. These are only visible if you open the file in WordPad.

Is it possible to open the file using WordPad, remove the last two characters from every line (except the header which is line 1) and then resave the file?

All help greatly appreciated.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
No, but it's a trivial exercise to do it using VBA.

Would you like me to provide some code?
 
Upvote 0
Bearing in mind these things sometimes take a couple of attempts to get right, try this against a copy of your CSV file - i.e. make a copy of it and run the code against that. It will prompt you to navigate your way to the file and then it makes a .BAK copy of it 'just in case'.

Code:
[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT] 
[FONT=Fixedsys]Public Sub ProcessTextFile()
 
  Dim sFileName As String
  Dim sBakFile As String
  Dim iPtr As Integer
  Dim iFHin As Integer
  Dim iFHout As Integer
  Dim sRecord As String
  Dim iRecords As Long
  Dim iChanged As Long
  
  ChDrive Left(ThisWorkbook.Path, 2)
  ChDir Mid(ThisWorkbook.Path, 3)
  sFileName = Application.GetOpenFilename(FileFilter:= _
              "CSV (Comma delimited) (*.csv), *.csv, Text (*.txt), *.txt, All files (*.*), *.*")
  If sFileName = "False" Then Exit Sub
  
[COLOR=green]  ' make a filename based on the name of the input file but with the extension BAK: we will use
  ' this as our input file, so if anything goes wrong the user still has a copy of his original
  ' file intact and unmodified
[/COLOR]  iPtr = InStrRev(sFileName, ".")
  If iPtr = 0 Then
    sBakFile = sFileName & ".bak"
  Else
    sBakFile = Left(sFileName, iPtr - 1) & ".bak"
  End If
  FileCopy sFileName, sBakFile
  
  iRecords = 0
  iChanged = 0
  
  Close
  iFHin = FreeFile()
  Open sBakFile For Input As iFHin
  iFHout = FreeFile()
  Open sFileName For Output As iFHout
  
[COLOR=green]  ' get the header line and write it out unchanged
[/COLOR]  Line Input #iFHin, sRecord
  Print #iFHout, sRecord
  
  Do Until EOF(iFHin)
    Line Input #iFHin, sRecord
    If Len(sRecord) > 1 Then
[COLOR=green]      ' if there are at least two characters in the line, remove the last two
[/COLOR]      sRecord = Left(sRecord, Len(sRecord) - 2)
      iChanged = iChanged + 1
    End If
    Print #iFHout, sRecord
    iRecords = iRecords + 1
  Loop
  
  Close iFHin
  Close iFHout
  
  MsgBox "Done!" & Space(10) & vbCrLf & vbCrLf _
       & "Header plus " & Format(iRecords, "#,##0") & " records read" _
       & Space(10) & vbCrLf & vbCrLf _
       & Format(iChanged, "#,##0") & " records changed" & Space(10), _
       vbOKOnly + vbInformation
 
End Sub[/FONT]
 
Upvote 0
Hi Ruddles

I've just tested your code and it doesn't update anything. It's hittin EOF as soon as the headers are written.
I think it's because the file is missing line breaks due to the last two characters being in place.

Is there any way of updating the code to take account of missing line breaks?
 
Upvote 0
I think need to know the exact structure of the file. When you said "remove the last two characters from every line", that suggested there was more than one line in it. What are these "two characters"? Are they in place of the normal carriage-return/line-feed?

Can you provide a hex dump of the file? Or send me the file itself?

(I shall PM you my email addy JIC.)
 
Upvote 0
The last two characters are always a comma and a set of speech marks (i.e. ,").

It looks like these are in place of the normal carriage returns/line feeds.
Unfortunately, just to make life harder, every field has a set of speech around it so I can't just do a find/replace to remove them unless you can think of some way around it.
 
Upvote 0
Richard

Perhaps these characters are the line breaks?

Are you trying to import this code into Excel?

By the way, why do the quotes (speech marks) prevent a find and replace?
 
Upvote 0
@Norie
Each field is surrounded by speech marks, so you get:-
"field1","field2","field3"

the file at the moment looks like this:-
"field1","field2","field3",""field1 of next line","field2 of next line","field3 of next line","
and so on.
There are strange characters in each record (it appears as a small square in Notepad but doesn't appear in Wordpad) but there's very little I can recognise as a line break.

The information is to be imported into Access into a linked table.

How would I remove the extra ," from each line (highlighted in red above)?

::edit::

I've just imported one of the wierd characters into Excel by pasting it directly into the formula bar instead of inst a cell and it's character code 10 which, from Google, is a line break.

It looks like it's just the extra ," that's causing the issue here.
 
Last edited:
Upvote 0
Richard

To replace that you could replace ,"" with ,".

Or even just "" with " (double-quote with single quote).

I don't know if that will help with the 'hidden' character though.

Where does the data actually come from originally and what is it you are trying to import, eg a text file?
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top