Replace double quotes in a text file?

wessir
wessir used Ask the Experts™
on
I have a need to read text files into an ADO recordset.  Some of my files have double quotes in them (out of my control to prevent that) and when reading those records into my recordset the double quote causes a misread of the record (record ends at first double quote " in source file line).

Is there a way to use the streamreader and streamwriter to create a new file from the input file, read it one character at a time, and replace any undesired characters - in this case the double quotes would need to be replaced by single quotes.

Could that also be used to replace by hex code of the character?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
>read it one character at a time,
I would not read characters by character, but line by line.
the string.Replace() function can return a string with characters replaced.
if the criteria is a bit more complex, you might use regular expression objects to replace "some" of the double quotes.

Author

Commented:
Oh! How did I miss that.  Thanks!  Here's my code using your suggestion.
        Dim sr As New StreamReader(strInputFile)
        Dim sw As New StreamWriter(strOutputFile)
        Dim str As String
        Do While sr.Peek() >= 0
            str = sr.ReadLine.Replace(Chr(34), Chr(39))
            sw.WriteLine(str)
        Loop
        sr.Close()
        sw.Close()
        sr.Dispose()
        sw.Dispose()
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
Grading comment:
Oh! How did I miss that. Thanks! Here's my code using your suggestion.

Dim sr As New StreamReader(strInputFile)
Dim sw As New StreamWriter(strOutputFile)
Dim str As String
Do While sr.Peek() >= 0
str = sr.ReadLine.Replace(Chr(34), Chr(39))
sw.WriteLine(str)
Loop
sr.Close()
sw.Close()
sr.Dispose()
sw.Dispose()

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial