VBS批量替换文件夹下的文件内容后(中文)出现乱码,求原因,十万火急。

Shawlj 2018-04-12 01:09:52
被替换的文件编码是UTF-8,但是替换完成后的文件变成了ANSI编码了。

Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run "cmd /c dir /s/b *.cpt > list.txt",vbHide
Wscript.Sleep 1000
'WshShell.Charset = "UTF-8"
sFile = "list.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'objFSO.Charset = "UTF-8"
Set oFile = objFSO.OpenTextFile(sFile,1)
Do While Not oFile.AtEndOfStream
strLine = oFile.ReadLine
If Len(strLine) > 0 Then
Set File = objFSO.OpenTextFile(strLine, 1)
aryLines = File.ReadAll
File.Close
aryLines = Replace(aryLines, "ORCL_DS_MES", "SHLY_JIT_UAT")
Set File = objFSO.OpenTextFile(strLine, 2)
File.Write aryLines
File.Close
End If
Loop
oFile.Close
objFSO.DeleteFile sFile
Set objFSO = Nothing
...全文
983 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Shawlj 2018-04-14
  • 打赏
  • 举报
回复
谢谢#2楼朋友,得分结帖。
Function LoadText(path, encoding)
	With CreateObject("ADODB.Stream")
		.Mode = 3
		.Type = 1
		.Open
		.LoadFromFile path
		.Position = 0
		.Type = 2
		.Charset = encoding
		.Position = 0
		LoadText = .ReadText(-1)
		.Close
	End With
End Function

Sub SaveAs(text, path, encoding)
	With CreateObject("ADODB.Stream")
		.Mode = 3
		.Type = 2
		.Open
		.Charset = encoding
		.WriteText text
		.SaveToFile path, 2
		.Close
	End With
End Sub


Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run "cmd /c dir /s/b *.cpt > list.txt",vbHide
Wscript.Sleep 1000
'WshShell.Charset = "UTF-8"
sFile = "list.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'objFSO.Charset = "UTF-8"
Set oFile = objFSO.OpenTextFile(sFile,1)
Do While Not oFile.AtEndOfStream
      strLine = oFile.ReadLine
      If Len(strLine) > 0 Then
            aryLines = LoadText(strLine, "UTF-8")
            aryLines = Replace(aryLines, "SHLY_JIT_UAT", "ORCL_DS_MES")
            SaveAs aryLines, strLine, "UTF-8"
      End If
Loop
oFile.Close
objFSO.DeleteFile sFile
Set objFSO = Nothing
wcwtitxu 2018-04-12
  • 打赏
  • 举报
回复
Function LoadText(path, encoding)
	With CreateObject("ADODB.Stream")
		.Mode = 3
		.Type = 1
		.Open
		.LoadFromFile path
		.Position = 0
		.Type = 2
		.Charset = encoding
		.Position = 0
		LoadText = .ReadText(-1)
		.Close  ' 上边漏了 close
	End With
End Function
wcwtitxu 2018-04-12
  • 打赏
  • 举报
回复
utf-8 要用 adodb.stream 读写, fso 读写只能本地编码或者 unicode




Function LoadText(path, encoding)
	With CreateObject("ADODB.Stream")
		.Mode = 3
		.Type = 1
		.Open
		.LoadFromFile path
		.Position = 0
		.Type = 2
		.Charset = encoding
		.Position = 0
		LoadText = .ReadText(-1)
	End With
End Function

Sub SaveAs(text, path, encoding)
	With CreateObject("ADODB.Stream")
		.Mode = 3
		.Type = 2
		.Open
		.Charset = encoding
		.WriteText text
		.SaveToFile path, 2
		.Close
	End With
End Sub


Set WshShell = CreateObject("Wscript.Shell")
WshShell.Run "cmd /c dir /s/b *.cpt > list.txt",vbHide
Wscript.Sleep 1000
'WshShell.Charset = "UTF-8"
sFile = "list.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
'objFSO.Charset = "UTF-8"
Set oFile = objFSO.OpenTextFile(sFile,1)
Do While Not oFile.AtEndOfStream
      strLine = oFile.ReadLine
      If Len(strLine) > 0 Then
            aryLines = LoadText(strLine, "UTF-8")
            aryLines = Replace(aryLines, "ORCL_DS_MES", "SHLY_JIT_UAT")
            SaveAs aryLines, strLine, "UTF-8"
      End If
Loop
oFile.Close
objFSO.DeleteFile sFile
Set objFSO = Nothing

4,008

社区成员

发帖
与我相关
我的任务
社区描述
它是一种微软环境下的轻量级的解释型语言,它使用COM组件、WMI、WSH、ADSI访问系统中的元素,对系统进行管理。
社区管理员
  • vbScript社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧