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 ÿ
'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.
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)
Note that VBScript does not actually support UTF-8. It cannot decode UTF-8 and display the correct character.
The example above works correctly whether the UTF-8 file is saved with or without BOM. The search, replace, and file write all work fine because a replace of 🙂
with 👍
is just F0 9F 99 82
being replaced by F0 9F 91 8D
.
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