-1

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?

Nic3500
  • 8,144
  • 10
  • 29
  • 40
Geoff Vane
  • 33
  • 1
  • 8
  • 2
    What is the encoding used in your text file? What does it contain? If you're not sure, please show us the contents of it _in hex_. – 41686d6564 stands w. Palestine Aug 13 '18 at 18:37
  • It could just be a binary file, in which case `OpenTextFile()` is no use. You would need to use an `ADODB.Stream` to read the binary file properly. – user692942 Aug 13 '18 at 18:48
  • Answer below: type of text can not be selected if the boolean is forgotten Thanks for your answers! – Geoff Vane Aug 13 '18 at 19:12
  • Possible duplicate of [How to read from a binary file using VBScript](https://stackoverflow.com/questions/37477243/how-to-read-from-a-binary-file-using-vbscript) – user692942 Aug 14 '18 at 08:37

2 Answers2

1

Darn! The boolean option within OpenTextFile examples is often left out!

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

Path is the path to the file. ForReading should be 1 for read only.

Then this False is the often omitted boolean (false means it's not written )

Only when the boolean is added correctly, you can pick a type of txt file.

In my case unicode so I pick -1 for the Tristate.

Tip: if you ever get weird results with textfiles, open in notepad, choose save as and then it will reveal what kind of text you actually have.

Geoff Vane
  • 33
  • 1
  • 8
  • It's not working. I need to send txt files as binary, as I need to check the file length against that on the FTP server. Alas I get a mix of UTF-8 and ANSI as the end result which is worthless. – Geoff Vane Aug 13 '18 at 20:59
  • You haven't explained to anyone what type of file it is and what result you're expecting, so honestly I'm not surprised it isn't working. – user692942 Aug 14 '18 at 07:20
  • To me it seems that MS itself set a wrong example in here https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/opentextfile-method – M at Sep 20 '20 at 07:22
-1

Your problem can be because a lot of thigs like the encode of target file, one of the most common encode us UTF-8 you can chage it with notepad++:

How do I convert an ANSI encoded file to UTF-8 with Notepad++?

I think you should put some validation code to find the real problem, I suggest this code:

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
qoixiop
  • 11
  • 2
  • Thanks, but if you add the mentioned boolean, you can use any type of textfile you want. But without adding the boolean, the tristate option ends at the wrong position, leading to weird results! – Geoff Vane Aug 13 '18 at 19:01