这是我的代码:
Set fso = CreateObject("Scripting.FileSystemObject")
strText = fso.OpenTextFile(strLocalFolderName & "\" & Oudste).ReadAll()
msgbox strText
但是strText
在这些行之后包含垃圾。
怎么可能呢?
答案 0 :(得分:0)
该死! OpenTextFile示例中的布尔选项经常被忽略!
fso.OpenTextFile(Path, ForReading, False, TriStateTrue)
路径是文件的路径。 ForReading应该为1(只读)。
然后,这个False是经常被忽略的布尔值(false表示未写入)
只有正确添加布尔值后,您才能选择一种txt文件。
在我的情况下是unicode,所以我为Tristate选择-1。
提示:如果使用文本文件得到奇怪的结果,请在记事本中打开,选择另存为,然后它将显示您实际拥有的文本类型。
答案 1 :(得分:-1)
您的问题可能是因为许多杂物,例如目标文件的编码,是最常见的UTF-8编码之一,您可以使用notepad ++进行修改:
How do I convert an ANSI encoded file to UTF-8 with Notepad++?
我认为您应该放置一些验证代码来找到真正的问题,我建议使用以下代码:
ForReading=1 'Open a file for reading only. You can't write to this file.
ForWriting=2 'Open a file for writing.
ForAppending=8 'Open a file and write to the end of the file.
CreateIfNotExist=TRUE 'If you use FALSE you get error if not exist
set fso = CreateObject("Scripting.FileSystemObject")
if (fso.fileexists(".\test.txt")) then
set ts = fso.OpenTextFile(".\test.txt", ForReading, CreateIfNotExist)
if NOT ts.AtEndOfStream then
s = ts.ReadAll
msgbox s
else
msgbox "End of file"
end if
else
msgbox "File not found"
end if