Hydrogen-4
Hydrogen-4

Reputation: 207

VBS Scripting.FileSystemObject Need help writing ANSI txt File

I am trying to create an ANSI file. But everything I try gives me an UTF-8 w/o BOM.

I have tried to do this with the least amount of code for testing purpose and this is what I found.

 Dim myFile 
 Dim objFSO : Set objFSO=CreateObject("Scripting.FileSystemObject")
 Set myFile = objFSO.CreateTextFile("C:\TestFile.txt")
 myFile.Close

The above code gives me an ANSI file as advertised by MSDS but when i write to it in the bellow code

 Dim myFile 
 Dim objFSO : Set objFSO=CreateObject("Scripting.FileSystemObject")
 Set myFile = objFSO.CreateTextFile("C:\TestFile.txt")
 myFile.WriteLine "TestLine"
 myFile.Close

When inspecting the file using Notepad++ now I have UTC-8 w/o BOM

There is code awaiting for the actual file I am trying to create. the code was not accepting the file. after some tedious debugging I found that it does not like the UTF format. If I save as ANSI using Notepad++ now the code accepts the file. I need to do this programmatically from the go.

Can anyone confirm if they get the same results? How do I make sure I end up with an ANSI text file? Thank you

Upvotes: 2

Views: 10707

Answers (1)

JosefZ
JosefZ

Reputation: 30113

Common syntax: object.CreateTextFile(filename[, overwrite[, unicode]]). Here

  • overwrite: Optional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten. Note:If the overwrite argument is false, or is not provided, for a filename that already exists, an error occurs.
  • unicode: Optional. Boolean value that indicates whether the file is created as a Unicode or ASCII file. The value is true if the file is created as a Unicode file, false if it's created as an ASCII file. If omitted, an ASCII file is assumed. (but last statement seems to contradict your experience...)

So you could open your file with

Set myFile = objFSO.CreateTextFile("C:\TestFile.txt", true, false)

If it fails, use object.OpenTextFile(filename[, iomode[, create[, format]]]), where

  • object: Required. Object is always the name of a FileSystemObject.
  • filename: Required. String expression that identifies the file to open.
  • iomode: Optional. Can be one of three constants: ForReading, ForWriting, or ForAppending.
  • create: Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created, False if it isn't created. If omitted, a new file isn't created.
  • format: Optional. One of three TriState values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

You could use literal values for iomode, create and format arguments, or define (and use) next constants:

'various useful constants
'iomode
Const ForReading = 1, ForWriting = 2, ForAppending = 8
'create
Const DontCreate =  False ' do not create a new file if doesn't exist
Const CreateFile =  True  ' create a new file if the specified filename doesn't exist
'format
Const OpenAsDefault = -2  ' Opens the file using the system default.
Const OpenAsUnicode = -1  ' Opens the file as Unicode.
Const OpenAsUSAscii =  0  ' Opens the file as ASCII.

'TriState (seen in documetation)
Const TristateUseDefault  = -2 ' Opens the file using the system default.
Const TristateTrue        = -1 ' Opens the file as Unicode.
Const TristateFalse       =  0 ' Opens the file as ASCII.

Then you could open your file with (but ensure to delete existing one if exists!)

Set myFile = objFSO.OpenTextFile( "C:\TestFile.txt", 2, true, 0)

or (more readable)

Set myFile = objFSO.OpenTextFile( "C:\TestFile.txt" _
             , ForWriting, CreateFile, OpenAsUSAscii)

Upvotes: 3

Related Questions