-1

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.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
John D
  • 139
  • 13
  • 2
    Please read the description for a tag before using it. VBScript, VB6 and VB.NET are all different languages and each tag specifies that it must not be used for questions about the others. Don't spam tags and try to trick people into reading questions irrelevant to them. People watch specific tags for a reason. Based on the title, I'm assuming VBScript and removing the other tags. – jmcilhinney May 25 '23 at 13:29
  • Is the file you're editing encoded as ANSI, UTF-8 or UTF-16? – LesFerch May 25 '23 at 14:34
  • Took a look in Notepad++ and show UTF-8 – John D May 25 '23 at 14:42
  • 1
    Note that the [Chr](https://ss64.com/vb/chr.html) and [Chrw](https://ss64.com/vb/chrw.html) functions take a decimal number as their argument, not hex. The `ÿ` character is code 255 in ANSII. In UTF-8 it may be represented by code 255 or the code pair C3 BF (195 191). You can't specify a code pair with Chrw. Use the actual character itself in the replace: `ÿ`. – LesFerch May 25 '23 at 14:47
  • 1
    Most answers you'll find will say that VBScript does not directly support UTF-8, so you will need to use the ADODB object. But VBScript will default to UTF-8 (for the functions needed in this case) as long as the script itself is saved as UTF-8. – LesFerch May 25 '23 at 14:49
  • Also relevant - [cscript.exe doesn't read unicode script (UTF-8)](https://stackoverflow.com/q/33236085) – user692942 May 26 '23 at 08:10

1 Answers1

0

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
LesFerch
  • 1,540
  • 2
  • 5
  • 21