设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1995|回复: 0
打印 上一主题 下一主题

[ADO/DAO] 将ASCII文件批量转为UTF-8格式

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2015-7-17 10:03:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. on error resume next
  2. Set WshShell=WScript.CreateObject("Shell.Application")
  3. dirPath=WshShell.BrowseForFolder(0, "请选择路径", 0, "").items().item().path
  4. if right(dirPath,1)<>"" then dirPath=dirpath&""
  5. ma=inputbox("请输入要转换为的编码","","Unicode")
  6. if ma="" or dirPath="" or msgbox("在使用前请确认已备份文件夹"&dirPath,1)=2 then WScript.Quit

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

  17. '检测文件的编码
  18. Function CheckCode (FileUrl)
  19. Dim slz
  20. set slz = CreateObject("Adodb.Stream")
  21. slz.Type = 1
  22. slz.Mode = 3
  23. slz.Open
  24. slz.Position = 0
  25. slz.Loadfromfile FileUrl
  26. Bin=slz.read(2)
  27. if AscB(MidB(Bin,1,1))=&HEF and AscB(MidB(Bin,2,1))=&HBB Then
  28. Codes="UTF-8"
  29. elseif AscB(MidB(Bin,1,1))=&HFF and AscB(MidB(Bin,2,1))=&HFE Then
  30. Codes="Unicode"
  31. else
  32. Codes="GB2312"
  33. end if
  34. slz.Close
  35. set slz = Nothing
  36. CheckCode=Codes
  37. End Function

  38. '以指定的编码读取文件
  39. Function ReadFile(FileUrl, CharSet)
  40. On Error Resume Next
  41. Dim Str
  42. Set stm = CreateObject("Adodb.Stream")
  43. stm.Type = 2
  44. stm.mode = 3
  45. stm.charset = CharSet
  46. stm.Open
  47. stm.loadfromfile FileUrl
  48. Str = stm.readtext
  49. stm.Close
  50. Set stm = Nothing
  51. wscript.echo Str
  52. ReadFile = Str
  53. End Function

  54. '以指定的编码写文件
  55. Function WriteToFile (FileUrl, Str, CharSet)
  56. On Error Resume Next
  57. Set stm = CreateObject("Adodb.Stream")
  58. stm.Type = 2
  59. stm.mode = 3
  60. stm.charset = CharSet
  61. stm.Open
  62. stm.WriteText Str
  63. stm.SaveToFile FileUrl, 2
  64. stm.flush
  65. stm.Close
  66. Set stm = Nothing
  67. End Function
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-1 22:46 , Processed in 0.091115 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表