a little background on my problem:
I have CSV file that is created by a query in MS Access. The main body of the CSV file contains columns; names, address, post codes, dates, etc. The last line of the file contains a summary of the data; filename, date, timestamp and total number of records. As all fields contain strings and we did not want the system to truncate or try and evaluate the dates and post codes, we've output these fields with quotation marks.
Last two lines look something like this:
"Name","Code","Address","Address2","Office","Location","City","State","PostCode","Date"
"FileName","DDMMYYYY","HH:MM:SS",TotalRecords,"","","","","",""
I need to remove the extra commas in the final line so it looks like this:
"FileName","DDMMYYYY","HH:MM:SS",TotalRecords
I've had a look through and tried the solution here: CSV file has extra commas
This creates a copy of my original file but leaves all the commas intact. This solution works if I remove ALL the quotation marks.
If you are in Excel you can do this single line in VBA:
I will explain from the middle out:
Replace all commas and double quotes with a space
Trim it ie remove starting and trailing spaces
Get the length of the trimmed text
Finally give me the left part of the ORIGINAL string with the char count set to what we worked out as the length minus commas and double quotes on the start and end.
Thanks everyone for your help.
I've used a different route to solve my problem. I created a batch script that extracts the records and reprint the data into a new file, ommitting the extra commas at the end of the file.
My code is below, any critique to improve it, is appreciated.
If a string ends with an unknown number of commas or an unknown number of:
for example:
Then this bit of VBA code will remove that junk: