VBS Scripting.FileSystemObject需要帮助编写ANSI txt文件 [英] VBS Scripting.FileSystemObject Need help writing ANSI txt File

查看:248
本文介绍了VBS Scripting.FileSystemObject需要帮助编写ANSI txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个ANSI文件。但我尝试的一切给我一个UTF-8 W / O BOM。



我试图用最少量的代码进行测试,这是我的

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

上面的代码给我一个ANSI文件作为广告的MSDS,但当我写在它下面的代码

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



当使用Notepad ++检查文件时, o BOM



我正在尝试创建的实际文件正在等待代码。代码不接受该文件。经过一些繁琐的调试后,我发现它不喜欢UTF格式。如果我使用Notepad ++保存为ANSI,现在代码接受该文件。我需要随时随地以编程方式做。



任何人都可以确认他们是否得到相同的结果?
如何确保我最终得到一个ANSI文本文件?
谢谢

解决方案

常用语法: object.CreateTextFile(filename [,overwrite [ unicode]])。这里




  • 覆盖
    可选。布尔值,指示是否可以覆盖现有文件。如果文件可以覆盖,值为 true ,如果无法覆盖,则 false 如果省略,现有文件不会被覆盖。 注意:如果覆盖参数 false ,或者未提供,对于已存在的文件名, strong>错误。

  • unicode
    可选。指示文件是作为Unicode或ASCII文件创建的布尔值。如果文件创建为Unicode文件,则值为 true ,如果创建为ASCII文件,则为 false 如果省略,则假设为ASCII文件。 (但最后一条语句似乎与您的体验相矛盾)



因此您可以使用

$ b $打开您的文件b

 设置myFile = objFSO.CreateTextFile(C:\TestFile.txt,true,false)
/ pre>

如果失败,使用 object.OpenTextFile(filename [,iomode [,create [,format]]]),其中




  • 对象:必需。对象始终是FileSystemObject的名称。

  • filename :必需。

  • iomode :可选。可以是三个常数之一: ForReading
    ForWriting ForAppending

  • 创建:可选。布尔值,指示如果指定的文件名不存在,是否可以创建一个新文件
    。如果创建了一个新文件,值为 True
    ,如果未创建,则为 False 。如果省略,不会创建
    新文件。

  • 格式:可选。三个 TriState 值之一,用于指示打开文件的
    格式。如果省略,则文件以ASCII格式打开。



您可以使用 iomode 创建格式参数或定义(和使用)下列常数:

 '各种有用的常数
'iomode
Const ForReading = 1,ForWriting = 2,ForAppending = 8
'create
Const DontCreate = False如果不存在则不创建新文件
Const CreateFile = True如果指定的文件名不存在,则创建一个新文件
'format
Const OpenAsDefault = -2'使用系统默认值打开文件。
Const OpenAsUnicode = -1'打开文件为Unicode。
Const OpenAsUSAscii = 0'以ASCII格式打开文件。

'TriState(见documetation)
Const TristateUseDefault = -2'使用系统默认值打开文件。
Const TristateTrue = -1'打开文件为Unicode。
Const TristateFalse = 0'以ASCII格式打开文件。

然后您可以打开您的文件(但如果存在,请确保删除现有的文件)

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

或(更易读)

 设置myFile = objFSO.OpenTextFile(C:\TestFile.txt_ 
,ForWriting,CreateFile,OpenAsUSAscii)


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

解决方案

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)

这篇关于VBS Scripting.FileSystemObject需要帮助编写ANSI txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆