如何处理vbscript中的特殊字符[重复]

问题描述 投票:0回答:1

我正在编写一个查找和替换工作流程,它需要能够查找特殊字符,其中一个是上面有两个点的 y。

https://www.compart.com/en/unicode/U+00FF

我目前在 Notepad++ 中使用宏来执行此查找和替换操作,但在此过程中我需要更多自动化。

Notepad++中的字符代码显示ÿ

'Find and Replace 
Const ForReading = 1
Const ForWriting = 2

Set objFile = objFSO.OpenTextFile("C:\TEST\" & JobNo & " " & strfolder & "\CustomerOriginals\" & FN, ForReading)

strText = objFile.ReadAll
objFile.Close
strText = Replace(strText, ChrW(00FF), "")


Set objFile = objFSO.OpenTextFile("C:\TEST\" & JobNo & " " & strfolder & "\CustomerOriginals\" & FN, ForWriting)
objFile.WriteLine strText

objFile.Close

任何帮助将不胜感激。

vbscript
1个回答
0
投票

您可以使用 ADO 在 VBScript 中读取和写入 UTF-8 文件,按照 SO 上的其他答案: https://stackoverflow.com/a/4127011/15764378

https://stackoverflow.com/a/13855268/15764378

https://stackoverflow.com/a/15230319/15764378

但是只要脚本本身保存为 UTF-8,VBScript 就会按照这个问题处理 UTF-8 进行简单的读取、替换、写入。示例:

替换Char.vbs

Const ForReading = 1
Const ForWriting = 2
Set oFSO = CreateObject("Scripting.FileSystemObject")
FP = "C:\Test\Test.txt"
Text = oFSO.OpenTextFile(FP,ForReading).ReadAll
Text = Replace(Text, "ÿ", "")
Text = Replace(Text, "🙂", "👍")
oFSO.OpenTextFile(FP,ForWriting).Write(Text)

请注意,VBScript 实际上并不支持 UTF-8。它无法解码 UTF-8 并显示正确的字符。

上面的示例可以正常工作,因为脚本保存为 UTF-8 without BOM,因此被简单地视为 ANSI。搜索、替换和文件写入都工作正常,因为将

🙂
替换为
👍
只是将
F0 9F 99 82
替换为
F0 9F 91 8D

您可能还想考虑使用 PowerShell 进行字符替换。这是一个例子:

替换Char.ps1

$FP = 'C:\Test\Test.txt'
$Text = Get-Content -Path $FP
$Text = $Text -replace 'ÿ', ''
Set-Content -Path $FP -Value $Text
© www.soinside.com 2019 - 2024. All rights reserved.