如何在VBScript中处理特殊字符

huangapple go评论56阅读模式
英文:

How to process special characters in vbscript

问题

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

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

我目前在Notepad++中使用宏来进行查找和替换,但我需要在这个过程中更多的自动化。

Notepad++中的字符代码显示为&#x00FF

'查找和替换
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(&H00FF), "")

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

objFile.Close

任何帮助将不胜感激。

英文:

I am writing a find and replace workflow and it needs to be able to find special characters, one of which is a y with two dots above it.

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

I currently use a MACRO in Notepad++ to do this find and replace, but I need a bit more automation in this process.

The character code in Notepad++ shows &#x00FF

'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

Any help would be greatly appreciated.

答案1

得分: 0

你可以使用ADO在VBScript中读取和写入UTF-8文件,参考StackOverflow上的其他答案:

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

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

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

但是,只要脚本本身以UTF-8格式保存,VBScript就可以处理UTF-8进行简单的读取、替换和写入,例如:

ReplaceChar.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)

你也可以考虑使用PowerShell来进行字符替换。以下是一个示例:

ReplaceChar.ps1

$FP = 'C:\Test\Test.txt'
$Text = Get-Content -Path $FP
$Text = $Text -replace 'ÿ', ''
Set-Content -Path $FP -Value $Text
英文:

You can use ADO to read and write UTF-8 files in VBScript, as per other answers on SO:
https://stackoverflow.com/a/4127011/15764378

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

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

But VBScript will handle UTF-8 for a simple read, replace, write as per this question, as long as the script itself is saved as UTF-8. Example:

ReplaceChar.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)

You may also want to consider using PowerShell to do the character replacement. Here's an example:

ReplaceChar.ps1

$FP = 'C:\Test\Test.txt'
$Text = Get-Content -Path $FP
$Text = $Text -replace 'ÿ', ''
Set-Content -Path $FP -Value $Text

huangapple
  • 本文由 发表于 2023年5月25日 21:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定