700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vba判断文件编码格式_VBA 把目录下所有CSV文件的编码由UTF-8转换成ANSI

vba判断文件编码格式_VBA 把目录下所有CSV文件的编码由UTF-8转换成ANSI

时间:2019-02-22 22:16:59

相关推荐

vba判断文件编码格式_VBA 把目录下所有CSV文件的编码由UTF-8转换成ANSI

展开全部

给你编个VBS版的,但又没有文件,没有测试,请上传个文件让我试下?32313133353236313431303231363533e4b893e5b19e31333335326232以下文件复制到文本文件中,另存为 ===> 编码转换.vbs,即可。OnErrorResumeNext

SetWshShell=wscript.CreateObject("Shell.Application")

dirpath=WshShell.BrowseForFolder(0,"请选择路径",0,"").items().Item().Path

IfRight(dirpath,1)<>"\"Thendirpath=dirpath&"\"

ma=InputBox("请输入要转换为的编码","","Unicode")

Ifma=""Ordirpath="\"OrMsgBox("在使用前请确认已备份文件夹"&dirpath,1)=2Thenwscript.Quit

'遍历文件夹下的文件

SetFSO=CreateObject("scripting.filesystemobject")

Setf=FSO.GetFolder(dirpath)

Setfs=f.Files

ForEachfileNInfs

FN=dirpath&fileN.Name&""

If".txt"=LCase(Right(FN,4))ThenCallWriteToFile(FN,ReadFile(FN,CheckCode(FN)),ma)

Next

SetFSO=Nothing

wscript.echo"全部成功"

'检测文件的编码

FunctionCheckCode(FileUrl)

Dimslz

Setslz=CreateObject("Adodb.Stream")

slz.Type=1

slz.Mode=3

slz.Open

slz.Position=0

slz.LoadFromFileFileUrl

Bin=slz.read(2)

IfAscB(MidB(Bin,1,1))=&HEFAndAscB(MidB(Bin,2,1))=&HBBThen

Codes="UTF-8"

ElseIfAscB(MidB(Bin,1,1))=&HFFAndAscB(MidB(Bin,2,1))=&HFEThen

Codes="Unicode"

Else

Codes="GB2312"

EndIf

slz.Close

Setslz=Nothing

CheckCode=Codes

EndFunction

'以指定的编码读取文件

FunctionReadFile(FileUrl,CharSet)

OnErrorResumeNext

DimStr

Setstm=CreateObject("Adodb.Stream")

stm.Type=2

stm.Mode=3

stm.CharSet=CharSet

stm.Open

stm.LoadFromFileFileUrl

Str=stm.readtext

stm.Close

Setstm=Nothing

wscript.echoStr

ReadFile=Str

EndFunction

'以指定的编码写文件

FunctionWriteToFile(FileUrl,Str,CharSet)

OnErrorResumeNext

Setstm=CreateObject("Adodb.Stream")

stm.Type=2

stm.Mode=3

stm.CharSet=CharSet

stm.Open

stm.WriteTextStr

stm.SaveToFileFileUrl,2

stm.flush

stm.Close

Setstm=Nothing

EndFunction

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。