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

时间:2015-03-09 01:49:29

标签: vbscript

我有一些脚本将字符串转换为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

2 个答案:

答案 0 :(得分:1)

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

allTextArray = SPLIT(originalText, vbCr)
For i=0 to UBound(allTextArray)
    allTextWithQuotes = allTextWithQuotes & """" & allTextArray(i) & """"
Next

答案 1 :(得分:1)

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"

这将使用正则表达式对象将所有中间行结尾替换为带有行的终止/开始和两个附加引号,一个在开头,一个在结尾。