首页 > 解决方案 > VBS OpenTextFile returns unexpected result

问题描述

This is my code:

Set fso = CreateObject("Scripting.FileSystemObject")
strText = fso.OpenTextFile(strLocalFolderName & "\" & Oudste).ReadAll()
msgbox strText

But strText contains rubbish after these lines. How can that be?

标签: textvbscript

解决方案


该死!OpenTextFile 示例中的布尔选项经常被忽略!

fso.OpenTextFile(Path, ForReading, False, TriStateTrue)

路径是文件的路径。对于只读,ForReading 应该为 1。

那么这个 False 是经常被省略的布尔值(false 表示它没有被写入)

只有正确添加布尔值时,您才能选择一种txt文件。

在我的例子中是 unicode,所以我选择 -1 作为 Tristate。

提示:如果您使用文本文件得到奇怪的结果,请在记事本中打开,选择另存为,然后它会显示您实际拥有的文本类型。


推荐阅读