1

I got some script to convert string to base64 and write encoded data to text file that's all goes fine and result stored in encoded.txt but I need each line to be double quoted at the start and '& _' with double quotes at the end so how to make this script do that automatically for each line? here is my script

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 Answers 2

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"

This will use a regular expression object to replace all intermediate line endings with the adecuated line termination/start and two aditional quotes, one at the start and one at the end.

Sign up to request clarification or add additional context in comments.

Comments

1

Split the text by a line break, then loop through and put it all back together while you add the quotes. (not tested, but probably close, code follows; there is a strong chance you'll need to find the right type of line feed for your situation - I picked vbCr as an example):

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.