如何删除文本文件VBScript中的换行符

时间:2023-01-16 19:55:58

I have numerous CSV files and they don't import correctly to excel, im just asking is there a way to remove the line breaks at the end of the strLine using VBScript?

我有很多CSV文件,他们没有正确导入到Excel,我只是问有没有办法在使用VBScript的strLine结束时删除换行符?

1 个解决方案

#1


2  

Yes, you can remove line breaks from CSVs. ReadLine will automatically remove line breaks from the end of the line:

是的,您可以从CSV中删除换行符。 ReadLine将自动从行尾删除换行符:

Set fso = CreateObject("Scripting.FileSystemObject")

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

f1 = fso.OpenTextFile(filename)
f2 = fso.OpenTextFile(filename & ".tmp", 2, True)
Do Until f1.AtEndOfStream
  f2.Write f1.ReadLine
Loop
f1.Close
f2.Close

Or you could read the whole file and replace the line breaks:

或者您可以读取整个文件并替换换行符:

Set fso = CreateObject("Scripting.FileSystemObject")

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

text = fso.OpenTextFile(filename).ReadAll
fso.OpenTextFile(filename, 2).Write Replace(text, vbNewLine, ",")

However, that might be addressing a symptom rather than the cause. I second Ekkehard.Horner's comment: for a better answer you should definitely provide more details about your input file and the way the import fails.

然而,这可能是解决症状而不是原因。我的第二个Ekkehard.Horner的评论:为了更好的答案,你绝对应该提供有关输入文件和导入失败方式的更多细节。

#1


2  

Yes, you can remove line breaks from CSVs. ReadLine will automatically remove line breaks from the end of the line:

是的,您可以从CSV中删除换行符。 ReadLine将自动从行尾删除换行符:

Set fso = CreateObject("Scripting.FileSystemObject")

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

f1 = fso.OpenTextFile(filename)
f2 = fso.OpenTextFile(filename & ".tmp", 2, True)
Do Until f1.AtEndOfStream
  f2.Write f1.ReadLine
Loop
f1.Close
f2.Close

Or you could read the whole file and replace the line breaks:

或者您可以读取整个文件并替换换行符:

Set fso = CreateObject("Scripting.FileSystemObject")

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

text = fso.OpenTextFile(filename).ReadAll
fso.OpenTextFile(filename, 2).Write Replace(text, vbNewLine, ",")

However, that might be addressing a symptom rather than the cause. I second Ekkehard.Horner's comment: for a better answer you should definitely provide more details about your input file and the way the import fails.

然而,这可能是解决症状而不是原因。我的第二个Ekkehard.Horner的评论:为了更好的答案,你绝对应该提供有关输入文件和导入失败方式的更多细节。