vbscript - 使用vbs从文本文件中删除空字符

标签 vbscript null

我有大约 6MB 大小的文本文件。有些行包含我想删除的 NULL (Chr(0)) 字符。 我有两种方法可以做到这一点:使用 Asc()=0 但这需要大约 50 秒才能完成,另一种方法使用 InStr (line, Chr(0)) =0 (fast ~ 4sec) 但结果从包含 NULL 字符的行。

以文本文件的第一行为例:

@@MMCIBN.000NULL7NULL076059NULL7653NULL1375686349NULL2528NULL780608NULL10700NULL\NULL_NC_ACT.DIR\CFG_RESET.INI

第一种方法(有效但非常慢)

function normalise (textFile )

Set fso = CreateObject("Scripting.FileSystemObject")
writeTo = fso.BuildPath(tempFolder, saveTo & ("\Output.arc"))
Set objOutFile = fso.CreateTextFile(writeTo)
Set objFile = fso.OpenTextFile(textFile,1)

Do Until objFile.AtEndOfStream 
    strCharacters = objFile.Read(1)
    If Asc(strCharacters) = 0 Then
        objOutFile.Write ""
        nul = true
    Else
        if nul = true then
            objOutFile.Write(VbLf & strCharacters)
        else
            objOutFile.Write(strCharacters)
        end if
    nul = false
    End If
Loop

objOutFile.close
end function

输出看起来像这样:

@@MMCIBN.000
7
076059
7653
1375686349
2528
780608
10700
\
_NC_ACT.DIR\CFG_RESET.INI

第二种方法代码:

filename = WScript.Arguments(0)

Set fso = CreateObject("Scripting.FileSystemObject")

sDate = Year(Now()) & Right("0" & Month(now()), 2) & Right("00" & Day(Now()), 2)
file = fso.BuildPath(fso.GetFile(filename).ParentFolder.Path, saveTo & "Output " & sDate & ".arc")
Set objOutFile = fso.CreateTextFile(file)
Set f = fso.OpenTextFile(filename)

Do Until f.AtEndOfStream
    line = f.ReadLine

    If (InStr(line, Chr(0)) > 0) Then 
        line = Left(line, InStr(line, Chr(0)) - 1) & Right(line, InStr(line, Chr(0)) + 1)
    end if

    objOutFile.WriteLine line

Loop

f.Close

但随后的输出是:

@@MMCIBN.000\CFG_RESET.INI

有人可以指导我如何在不丢失信息的情况下快速删除 NULLS。我想尝试使用第二种方法来扫描哪些行号需要更新,然后将其提供给第一种方法以尝试加快速度,但老实说,我什至不知道从哪里开始这样做! 提前致谢...

最佳答案

看起来第一种方法只是将每个 NULL 替换为换行符。如果这就是您所需要的,您可以这样做:

更新:

好的,听起来您需要用换行符替换每组 NULL。让我们试试这个:

strText = fso.OpenTextFile(textFile, 1).ReadAll()

With New RegExp
    .Pattern = "\x00+"
    .Global = True
    strText = .Replace(strText, vbCrLf)
End With

objOutFile.Write strText

更新 2:

我认为 TextStream 类的 Read/ReadAll 方法在处理文本和二进制数据的混合时遇到问题。让我们使用 ADO Stream 对象来读取数据。

' Read the "text" file using a Stream object...
Const adTypeText = 2

With CreateObject("ADODB.Stream")
    .Type = adTypeText
    .Open
    .LoadFromFile textFile
    .Charset = "us-ascii"
    strText = .ReadText()
End With

' Now do our regex replacement...
With New RegExp
    .Pattern = "\x00+"
    .Global = True
    strText = .Replace(strText, vbCrLf)
End With

' Now write using a standard TextStream...
With fso.CreateTextFile(file)
    .Write strText
    .Close
End With

关于vbscript - 使用vbs从文本文件中删除空字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25555307/

相关文章:

email - 发送 HTML 电子邮件 asp

arrays - Swift - 访问 nil 不会崩溃

c++ - 指针在退出功能时设置为空指针

windows - 添加/删除程序在哪里提取 "Installed On"列的数据?

VBScript 如何从函数返回 ArrayList

powershell - 使用 VB 脚本运行窗口媒体播放器或 VLC

null - Webbrowser 控件的 window.external 始终为 null

mongodb - 转换一个并不总是存在的领域

java - null 是一个对象吗?

vbscript - 使用 vbscript 从本地驱动器获取文件夹列表