vbscript跳过线和cr行的结尾 [英] vbscript skipline and cr line endings

查看:105
本文介绍了vbscript跳过线和cr行的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当我使用objFile.ReadLine方法读取文本文件时,整个文件以长行形式返回.我正在打开的文件使用的是CR行尾,而不是CRLF或LF行尾.如果ReadLine无法识别CR行结尾,有人知道如何在vbscript中逐行读取此文件吗?代码如下:

For some reason when I use the objFile.ReadLine method to read my text file, the entire file is returned as one long line. The file I'm opening is using CR line endings instead of CRLF or LF line endings. Does anyone know how I can read this file line by line in vbscript if ReadLine does not recognize CR line endings? Code is below:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txsInput = objFSO.OpenTextFile("C:\Path\To\My\File.csv",1)
'Read lines one by one 
Do While txsInput.AtEndOfStream <> True
  strTemp = txsInput.ReadLine
  msgbox strTemp
Loop

我没有为每一行发出警报,而是收到整个文件的警报.感谢您的帮助.

Instead of alerting for each line, I get one alert with the entire file. Any help is appreciated.

推荐答案

.ReadLine在您的情况下不起作用,因为它取决于vbLf字符的存在.如果您不想重新编码换行符,则可以执行建议的 Panayot Karabakalov (如果文件是大文件),或读取整个文件并在vbCr处拆分(如果文件不是那么大):

.ReadLine won't work in your case, because it depends on the existence of a vbLf character. If you don't want to re-encode the line breaks you can do either what Panayot Karabakalov suggested (if the file is large), or read the entire file and split it at vbCr (if the file is not-so-large):

text = objFSO.OpenTextFile("C:\Path\To\My\File.csv").ReadAll
For Each line In Split(text, vbCr)
  MsgBox line
Next

可以像这样重新编码文件:

Re-encoding the file can be done like this:

Set fso = CreateObject("Scripting.FileSystemObject")

filename = "C:\path\to\your.csv"

Set infile  = fso.OpenTextFile(filename)
Set outfile = fso.OpenTextFile(filename & ".tmp", 2)

Do Until infile.AtEndOfStream
  c = infile.Read(1)
  If c = vbCr Then
    outfile.Write vbCrLf
  Else
    outfile.Write c
  End If
Loop

infile.Close
outfile.Close

fso.DeleteFile filename, True
fso.MoveFile filename & ".tmp", filename

或者您可以使用类似 recode 之类的东西进行转换.大多数文本编辑器(例如 Vim SciTE UltraEdit 和…)也可以进行这种转换.

Or you could use something like recode for the conversion. Most text editors (e.g. Vim, Notepad++, SciTE, UltraEdit, …) can do this kind of conversion, too.

这篇关于vbscript跳过线和cr行的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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