taoilike 发表于 2012-4-4 18:55:19

告诉大家关于保存本地文件时的模板问题(win7 64位 测试可行)

这里以保存为一个txt文件为例


首先新建文本文档
输入[标签:xx]
xx为采集内容规则的标签名
然后保存

复制下面内容:新建文本文档,保存,修改后缀为.vbs(目的:转化模板编码格式)双击运行,选择你要转换编码的文件模板,下一步即可。。。 至于火车头里的设置大家应该知道吧~

on error resume next
Set WshShell=WScript.CreateObject("Shell.Application")
dirPath=WshShell.BrowseForFolder(0, "请选择路径", 0, "").items().item().path
if right(dirPath,1)<>"\" then dirPath=dirpath&"\"
ma=inputbox("请输入要转换为的编码","","UTF-8")
if ma="" or dirPath="\" or msgbox("在使用前请确认已备份文件夹"&dirPath,1)=2 then WScript.Quit

'遍历文件夹下的文件
Set FSO = CreateObject("scripting.filesystemobject")
Set f = FSO.GetFolder(dirPath)
Set fs = f.files
For Each fileN in fs
FN=dirPath&fileN.name&""
if ".txt"=lcase(right(FN,4)) then Call WriteToFile(FN, ReadFile(FN, CheckCode(FN)), ma)
Next
Set FSO = Nothing
wscript.echo "全部成功"

'检测文件的编码
Function CheckCode (FileUrl)
Dim slz
set slz = CreateObject("Adodb.Stream")
slz.Type = 1
slz.Mode = 3
slz.Open
slz.Position = 0
slz.Loadfromfile FileUrl
Bin=slz.read(2)
if AscB(MidB(Bin,1,1))=&HEF and AscB(MidB(Bin,2,1))=&HBB Then
Codes="UTF-8"
elseif AscB(MidB(Bin,1,1))=&HFF and AscB(MidB(Bin,2,1))=&HFE Then
Codes="Unicode"
else
Codes="GB2312"
end if
slz.Close
set slz = Nothing
CheckCode=Codes
End Function

'以指定的编码读取文件
Function ReadFile(FileUrl, CharSet)
On Error Resume Next
Dim Str
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.loadfromfile FileUrl
Str = stm.readtext
stm.Close
Set stm = Nothing
wscript.echo Str
ReadFile = Str
End Function

'以指定的编码写文件
Function WriteToFile (FileUrl, Str, CharSet)
On Error Resume Next
Set stm = CreateObject("Adodb.Stream")
stm.Type = 2
stm.mode = 3
stm.charset = CharSet
stm.Open
stm.WriteText Str
stm.SaveToFile FileUrl, 2
stm.flush
stm.Close
Set stm = Nothing
End Function

taoilike 发表于 2012-4-4 18:55:41

嘿嘿,沙发自己坐~

zdh_zg 发表于 2012-4-4 18:59:01

学习{:soso_e179:}

taoilike 发表于 2012-4-4 19:00:46

zdh_zg 发表于 2012-4-4 18:59 static/image/common/back.gif
学习

感谢楼上支持。。。觉得论坛人很不活跃,,,特别是高手。。。呜呜,新手问题总是解决不了~唉。。。

zdh_zg 发表于 2012-4-4 19:04:48

是啊,高手都很忙啊!{:soso_e100:}

negatlov 发表于 2012-4-4 20:33:21

不错,好东西。最近也在努力研究这个

qq1564798051 发表于 2012-4-9 14:35:18

刚试了很多谢这位兄弟的分享,找了好几天了!开发商也够黑了教程就是说那个有什么功能,没有一个是完全教你怎么用的!

茅山道士 发表于 2012-4-9 17:24:23

不错,好东西。最近也在努力研究这个
页: [1]
查看完整版本: 告诉大家关于保存本地文件时的模板问题(win7 64位 测试可行)