AndyPerlitch
AndyPerlitch

Reputation: 4739

How can you convert LF's to CRLF in a text file using a vbscript?

I have a CSV file that gets generated by a Mac program (unfortunately, with little encoding flexibility) which writes LFs at the end of lines. Then a vbscript reads this file like so:

Set objTextFile = fso.OpenTextFile("the_file.csv", 1)
lineItemString = objTextFile.Readline

However, since it is looking for CRLF at the end of the lines, lineItemString contains the text of the entire file. Since this is a daily procedure, I'd like not to have to add an interim step of using some utility program that properly converts all the line endings to CRLF.

Is there a way to avoid this by doing this conversion from within my vbscript?

Thanks in advance!

Upvotes: 2

Views: 9536

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

This will replace each LF in a string with CRLF:

Replace(str, vbLf, vbCrLf)

Depending on how you want to process the file it might be easier to just read the entire file and split the content by vbLf, though.

Set objTextFile = fso.OpenTextFile("the_file.csv", 1)
For Each line In Split(objTextFile.ReadAll, vbLf)
  ' do stuff
Next

Upvotes: 4

Related Questions