如何使用 vbscript 在文本文件中将 LF 转换为 CRLF? [英] How can you convert LF's to CRLF in a text file using a vbscript?

查看:41
本文介绍了如何使用 vbscript 在文本文件中将 LF 转换为 CRLF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 Mac 程序生成的 CSV 文件(不幸的是,编码灵活性很小),它在行尾写入 LF.然后一个 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

然而,由于它在行尾寻找 CRLF,lineItemString 包含整个文件的文本.由于这是一个日常程序,我不想添加使用一些实用程序的临时步骤,以正确地将所有行结尾转换为 CRLF.

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.

有没有办法通过在我的 vbscript 中进行这种转换来避免这种情况?

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

提前致谢!

推荐答案

这会将字符串中的每个 LF 替换为 CRLF:

This will replace each LF in a string with CRLF:

Replace(str, vbLf, vbCrLf)

不过,根据您希望如何处理文件,可能更容易读取整个文件并按 vbLf 拆分内容.

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

这篇关于如何使用 vbscript 在文本文件中将 LF 转换为 CRLF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆