vbscript向base64转换后的字符串添加引号

vbscript adding qoutes to base64 converted string

我有一些脚本可以将字符串转换为 base64,并将编码数据写入文本文件,一切正常,结果存储在 encoded.txt 中,但我需要在每一行的开头和 '& _ ' 末尾有双引号那么如何让这个脚本为每一行自动执行?这是我的脚本

Option Explicit
Const fsDoOverwrite     = true  
Const fsAsASCII         = false 
Const adTypeBinary      = 1    
Dim objFSO
Dim objFileOut
Dim objXML
Dim objDocElem
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open()
objStream.LoadFromFile(Wscript.scriptfullname)
Set objXML = CreateObject("MSXml2.DOMDocument")
Set objDocElem = objXML.createElement("Base64Data")
objDocElem.dataType = "bin.base64"
objDocElem.nodeTypedValue = objStream.Read()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileOut = objFSO.CreateTextFile("encoded.txt", fsDoOverwrite, fsAsASCII)
objFileOut.Write objDocElem.text
objFileOut.Close()
Set objFSO = Nothing
Set objFileOut = Nothing
Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing

用换行符拆分文本,然后在添加引号时循环遍历并将它们全部放回一起。 (未经测试,但可能接近,代码如下;您很有可能需要为您的情况找到正确的换行类型——我选择 vbCr 作为示例):

allTextArray = SPLIT(originalText, vbCr)
For i=0 to UBound(allTextArray)
    allTextWithQuotes = allTextWithQuotes & """" & allTextArray(i) & """"
Next
Sub encodeFileBase64( inputFile, outputFile )
    Const fsDoOverwrite     = True  
    Const fsAsASCII         = False 
    Const adTypeBinary      = 1    
    Dim stream, strBuffer

    Set stream = WScript.CreateObject("ADODB.Stream")
    With stream
        .Type = adTypeBinary
        .Open
        .LoadFromFile inputFile 
    End With 

    With WScript.CreateObject("MSXML2.DOMDocument").CreateElement("Base64Data")
        .dataType = "bin.base64"
        .nodeTypedValue = stream.Read()
        strBuffer = .Text 
    End With

    stream.Close 

    With New RegExp 
        .Multiline = True
        .Global = True
        .IgnoreCase = False 
        .Pattern = "[\r\n]+(?=[0-9A-Za-z])"
        strBuffer = Chr(34) & .Replace(strBuffer, Chr(34) & " & _" & vbCrLf & Chr(34) ) & Chr(34)
    End With 

    WScript.CreateObject("Scripting.FileSystemObject").CreateTextFile( outputFile, fsDoOverwrite, fsAsASCII ).Write strBuffer 

End Sub

encodeFileBase64 WScript.ScriptFullName, WScript.ScriptFullName & ".b64"

这将使用正则表达式对象将所有中间行结尾替换为精简行 termination/start 和两个附加引号,一个在开头,一个在结尾。