侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

vbs实现unicode和ascii编码转换

框架(架构)  /  管理员 发布于 7年前   240

一、Copy a Unicode File to an ANSI File

WiToAnsi.vbs文件:

复制代码 代码如下:
' Utility to rewrite a Unicode text file as an ANSI text file
' For use with Windows Scripting Host, CScript.exe or WScript.exe
' Copyright (c) 1999, Microsoft Corporation
'
Option Explicit

' FileSystemObject.CreateTextFile and FileSystemObject.OpenTextFile
Const OpenAsASCII   = 0
Const OpenAsUnicode = -1

' FileSystemObject.CreateTextFile
Const OverwriteIfExist = -1
Const FailIfExist      = 0

' FileSystemObject.OpenTextFile
Const OpenAsDefault    = -2
Const CreateIfNotExist = -1
Const FailIfNotExist   = 0
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim argCount:argCount = Wscript.Arguments.Count
If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0
If (argCount = 0) Then
    Wscript.Echo "Utility to copy Unicode text file to an ANSI text file." &_
        vbNewLine & "The 1st argument is the Unicode text file to read" &_
        vbNewLine & "The 2nd argument is the ANSI text file to write" &_
        vbNewLine & "If the 2nd argument is omitted, the Unicode file will be replaced"
    Wscript.Quit 1
End If

Dim inFile, outFile, inStream, outStream, inLine, FileSys, WshShell
If argCount > 1 Then
    outFile = Wscript.Arguments(1)
    inFile  = Wscript.Arguments(0)
Else
    outFile = Wscript.Arguments(0)
    inFile  = outFile & ".tmp"
    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    WshShell.Run "cmd.exe /c copy " & outFile & " " & inFile, 0, True
End If

Set FileSys = CreateObject("Scripting.FileSystemObject")
Set inStream  = FileSys.OpenTextFile(inFile, ForReading, FailIfNotExist, OpenAsDefault)
Set outStream = FileSys.CreateTextFile(outFile, OverwriteIfExist, OpenAsASCII)
Do
    inLine = inStream.ReadLine
    outStream.WriteLine inLine
Loop Until inStream.AtEndOfStream
inStream.Close
outStream.Close
If argCount = 1 Then WshShell.Run "cmd.exe /c del " & inFile, 0

批处理中调用:
复制代码 代码如下:
cscript WiToAnsi.vbs [path to Unicode file][path to ANSI file]

二、Copy a ANSI File to an Unicode File

只需对OpenTextFile和CreateTextFile的打开方式做调整即可。

三、参考

http://msdn.microsoft.com/en-us/library/aa368046%28VS.85%29.aspx

四、OpenTextFile和CreateTextFile的使用

CreateTextFile 方法

创建指定文件并返回 TextStream 对象,该对象可用于读或写创建的文件。
复制代码 代码如下:
object.CreateTextFile(filename[, overwrite[, unicode]])

参数

object

必选项。应为 FileSystemObject 或 Folder 对象的名称。

filename

必选项。字符串表达式,指明要创建的文件。

overwrite

可选项。Boolean 值指明是否可以覆盖现有文件。如果可覆盖文件,该值为 True;如果不能覆盖文件,则该值为 False 。如果省略该值,则不能覆盖现有文件。

unicode

可选项。Boolean 值指明是否以 Unicode 或 ASCII 文件格式创建文件。如果以 Unicode 文件格式创建文件,则该值为 True;如果以 ASCII 文件格式创建文件,则该值为 False。如果省略此部分,则假定创建 ASCII 文件。

OpenTextFile 方法

打开指定的文件并返回一个 TextStream 对象,可以读取、写入此对象或将其追加到文件。
复制代码 代码如下:
object.OpenTextFile(filename[, iomode[, create[, format]]])

参数

object

必选项。应为 FileSystemObject 对象的名称。

filename

必选项。字符串表达式,指明要打开的文件名称。

iomode

可选项。输入/输出模式,是下列三个常数之一:ForReading,ForWriting,或 ForAppending。

create

可选项。Boolean 值,指出当指定的 filename 不存在时是否能够创建新文件。允许创建新文件时为 True,否则为 False。默认值为 False。

format

可选项。三个 Tristate 值之一,指出以何种格式打开文件。若忽略此参数,则文件以 ASCII 格式打开。

设置

iomode 参数可为下列设置之一:

数 值 描述
ForReading 1 以只读模式打开文件。不能对此文件进行写操作。
ForWriting 2 以只写方式打开文件。不能对此文件进行读操作。
ForAppending 8 打开文件并在文件末尾进行写操作。

format 参数可为下列设置之一:

常数 值 描述
TristateUseDefault -2 以系统默认格式打开文件。
TristateTrue -1 以 Unicode 格式打开文件。
TristateFalse  0 以 ASCII 格式打开文件。


  • 上一条:
    Cipher 在 NTFS 卷上显示或改变文件的加密
    下一条:
    WScript.Shell对象SpecialFolders属性未公开文档分享
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客