vbscript - 使用 VBScript 查找并替换 NUL 字符

标签 vbscript

我有一个文本文件,其中随机行中包含 NUL 字符。我想找到第一个 NUL 字符并从该 NUL 字符中删除整行,如下所示:

输入:

1 2 3 4 20170821NUL20170821NULNULNULNUL 123 NULNULNUL
1 2 3 4 20170821 20170821 6 7 10 123 10 11 13
1 2 3 4 20170821NUL20170821NULNULNULNUL 123 NULNULNUL
1 2 3 4 20170821NUL20170821NULNULNULNUL 123 NULNULNUL

输出:

1 2 3 4 20170821
1 2 3 4 20170821 20170821 6 7 10 123 10 11 13
1 2 3 4 20170821
1 2 3 4 20170821

我有以下方法将文本文件数据读取到变量并循环数据并替换 NUL:

sInfile = WScript.Arguments(1)

'Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFS = oFSO.OpenTextFile(sInfile)
sData = oFS.ReadAll
oFS.Close
Set oFS = Nothing

MsgBox("File Read Completed")

'Remove Rest of the line from NULL
Do While InStr(sData, "\00.*") > 0
    sData = Replace(sData, "\00.*", "")
Loop

'Cleanup and end
Set oFS = Nothing
WScript.Quit

脚本顺利通过,但我看不到数据有任何变化。

编辑1: 更新代码:

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

If (WScript.Arguments.Count > 0) Then
    sInfile = WScript.Arguments(0)
Else
    WScript.Echo "No filename specified."
    WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
    sOutfile = WScript.Arguments(1)
Else
    sOutfile = sInfile
End If

'Get the text file from cmd file
sInfile = Wscript.Arguments(1)
' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFS = oFSO.OpenTextFile(sInfile)
sData = oFS.ReadAll
oFS.Close
Set oFS = Nothing

' Remove Rest of the line from NULL
Set re = New RegExp
re.Pattern = Chr(0) & ".*"
re.Global  = True
sData = re.Replace(sData, "")

Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFS = Nothing
WScript.Quit

这是我给出的示例输入:

enter image description here

我希望看到如下输出:

enter image description here

但是我得到了以下输出:

੊ਊਊਊਊਊਊਊਊਊਊ

EDIT 2: I am not aware of hex editors. Here is the sample input of HextDump:

FF FE 4A 00 42 00 43 00 09 00 31 00 32 00 33 00 34 00 38 00 36 00 37 00 38 
00 09 00 38 00 37 00 09 00 30 00 09 00 30 00 09 00 31 00 32 00 33 00 09 00 
32 00 30 00 31 00 37 00 09 00 31 00 32 00 33 00 34 00 09 00 31 00 33 00 34 
00 32 00 30 00 09 00 32 00 30 00 31 00 37 00 30 00 38 00 30 00 39 00 09 00 
35 00 31 00 30 00 33 00 09 00 09 00 09 00 09 00 33 00 34 00 31 00 34 00 38 
00 38 00 09 00 32 00 09 00 32 00 30 00 31 00 37 00 09 00 38 00 09 00 31 00 
09 00 37 00 09 00 2D 00 32 00 36 00 34 00 30 00 09 00 2D 00 33 00 39 00 33 
00 2E 00 31 00 36 00 31 00 33 00 37 00 35 00 09 00 2D 00 33 00 33 00 32 00 
2E 00 34 00 36 00 38 00 35 00 37 00 39 00 09 00 41 00 30 00 31 00 31 00 32 
00 35 00 38 00 39 00 2F 00 33 00 34 00 31 00 34 00 38 00 38 00 2F 00 09 00 
09 00 09 00 09 00 09 00 09 00 09 00 09 00 32 00 09 00 09 00 09 00 32 00 31 
00 37 00 38 00 31 00 09 00 58 00 59 00 5A 00 09 00 58 00 59 00 5A 00 09 00 
58 00 59 00 5A 00 09 00 31 00 32 00 33 00 09 00 31 00 32 00 33 00 09 00 2D 
00 32 00 36 00 34 00 09 00 58 00 59 00 5A 00 09 00 31 00 09 00 31 00 09 00 
31 00 32 00 33 00 09 00 09 00 09 00 32 00 31 00 37 00 38 00 32 00 31 00 0D 
00 0A 00 41 00 42 00 43 00 09 00 31 00 32 00 33 00 34 00 38 00 36 00 37 00

以及我得到的输出的 HexDump FF FE 4A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A

最佳答案

您正在尝试为 Replace() 函数指定正则表达式模式,但这不起作用。一般来说,您根本不需要使用正则表达式。

这是非正则表达式代码:

With CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.Arguments(1), 1, False, 0)
    sData = ""
    If Not .AtEndOfStream Then sData = .ReadAll
    .Close
End With

a = Split(sData, vbCrLf)
For i = 0 To UBound(a)
    q = Instr(a(i), Chr(0))
    If q > 0 Then a(i) = Mid(a(i), 1, q - 1)
Next
sData = Join(a, vbCrLf)

这是正则表达式版本:

With CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.Arguments(1), 1, False, 0)
    sData = ""
    If Not .AtEndOfStream Then sData = .ReadAll
    .Close
End With

With CreateObject("VBScript.RegExp")
    .Pattern = "^(.*?)\x00.*$"
    .Global  = True
    .Multiline  = True
    sData = .Replace(sData, "$1")
End With

关于vbscript - 使用 VBScript 查找并替换 NUL 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45933250/

相关文章:

vbscript - 用于检查文件夹是否存在然后运行文件的 Vbs 脚本

excel - 我不知道 .FieldName

xml - 如何从vbs读取utf-8 xml并获得正确的字符编码

windows - vbs Msgbox 参数 262144

visual-studio - 编译经典的 ASP 文件

batch-file - 从任务计划程序的批处理文件调用的 vbscript 无法打开 Excel 工作簿

javascript - 自动执行 HTTP 请求

.net - 是否可以在 VBScript 中获取 .Net 字符串对象实例?

iis - 可捕获错误 0xC0000005

vbscript - 使用经典 ASP VBScript 时无法获取原始 POST 数据