0

I have a .txt with this format:

image

I can remove the NUL characters in Notepad++ like this:

Solution

So I read about it, and I found the solution to replace the regular expresion "\x00" for " " ... but I need to create a VBScript. I found examples like this one:

sPath = "C:\Users\AL\T_1538_89945.txt"
sContent = ReadTextFile(sPath, 0) ' lFormat -2 - System default, -1 - Unicode, 0 - ASCII
sContent = Replace(sContent, "\x00", " ")
WriteTextFile sContent, sPath, 0

Function ReadTextFile(sPath, lFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 1, False, lFormat)
        ReadTextFile = ""
        If Not .AtEndOfStream Then ReadTextFile = .ReadAll
        .Close
    End With
End Function

Sub WriteTextFile(sContent, sPath, lFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 2, True, lFormat)
        .Write sContent
        .Close
    End With
End Sub

But it doesn't work, can someone adapt the code so it can run it from that location cleaning those regular expressions?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Axel Vazquez
  • 9
  • 1
  • 5

1 Answers1

0

Replace this line sContent = Replace(sContent, "\x00", " ")

To this sContent = Replace(sContent, ChrW(0), " ")

You were trying to replace a string "\x00" and not the actual NULL char. I used ChrW() to replace that particular char with space. Null is represented by character 0 and hence now we are not replacing a string but NULL char.

Pankaj Jaju
  • 5,371
  • 2
  • 25
  • 41