I have a vbscript script that takes a .ico file and creates a desktop shortcut using it as its icon, but I want to be able to have everything the script needs stored in itself, if possible.
Is it possible to store a .ico file in a vbscript?
2.6k views Asked by Crattik AtThere are 3 answers
On
There's an easier (and much faster) method to convert binary data to Base64 in VBScript than by using the code @Hackoo found. You can take advantage of Microsoft's implementation of Base64 by using the MSXML2.DOMDocument class. Here's a script that takes a binary file c:\test.jpg and converts it to Base64. The resulting Base64-encoded string is saved in a text file (c:\out.txt). It uses an ADO Stream to read the file into a binary array and then passes that to the routine that uses a DOMDocument to convert that binary data to Base64-encoded text.
Const BINARY_FILE = "c:\test.jpg"
Const BASE64_FILE = "c:\out.txt"
With CreateObject("Scripting.FileSystemObject").CreateTextFile(BASE64_FILE, True)
.Write BinaryFileToBase64(BINARY_FILE)
.Close
End With
Function BinaryFileToBase64(strFileName)
With CreateObject("ADODB.Stream")
.Type = 1 ' Specify binary data (adTypeBinary)
.Open
.LoadFromFile strFileName
BinaryFileToBase64 = Base64Encode(.Read) ' Read binary contents into VT_UI1 | VT_ARRAY
End With
End Function
' This function accepts binary (VT_UI1 | VT_ARRAY) data and converts it to Base64-encoded text (Unicode string).
Function Base64Encode(BinaryData) ' As String
With CreateObject("MSXML2.DOMDocument.3.0").CreateElement("Base64")
.DataType = "bin.base64" ' Set the type of data the element should store
.NodeTypedValue = BinaryData ' Write the binary data
Base64Encode = .Text ' Read it back as text
End With
End Function
So you can use this script to convert any binary file to its Base64-encoded string representation. For example, this is Stack Overflow's icon, saved as a 12x15 bitmap:
Qk1SAgAAAAAAADYAAAAoAAAADAAAAA8AAAABABgAAAAAABwCAAAAAAAAAAAAAAAAAAAAAAAA
hoOChoOChoOChoOChoOChoOChoOChoOChoOChoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPl
3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlhoOChoOChoOChoOChoOChoOC3uPlhoOC3uPl3uPl
hoOC3uPl3uPl3uPl3uPl3uPl3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlcIyocIyocIyocIyo
cIyocIyo3uPlhoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPlwtTdn8DV3uPlhoOC3uPl3uPl
3uPl3uPl2uHknL/UcafJVpfCVJbCbKPI3uPl3uPl3uPl3uPl3+Tm3+TmVZfCXJvEeKvLr8na
3+Tmbq7cVKHZ3+Tm3+Tm3+Tm4eXn4eXn3ePm4eXn4eXnsM3iP5fYQZjXs8/jV6Tx097o4eXn
4ubo4ubo4ubo3uToY6jbN5PXdbLc3eToOJb0K4/0e63vdqrw4+fp4+fp4+fpQZjYRZvYwNbl
3uXpOJb0LZD00t7qMYP1lLvu5Ojq5Ojq5OjqxNjm5Ojq3+bqOpf0LpH01uHrcafwPInz5Ojq
5enr5enr5enr5enr5enrRJzzLpH01+Lr3OTrMIP1psbu5enr5+rs5+rs5+rs5+rs5+rsrs7u
3eXs5+rsZqLyQ4705+rs5+rs6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6OvtN4f0s83v6Ovt6Ovt
To decode the Base64-encoded string, we just need to perform the steps in reverse. First, we decode the text back into its original binary form. Then, we write that binary data to a file.
CONST NEW_BINARY_FILE = "c:\test2.jpg"
With CreateObject("Scripting.FileSystemObject").OpenTextFile(BASE64_FILE)
Base64ToBinaryFile .ReadAll(), NEW_BINARY_FILE
.Close
End With
Sub Base64ToBinaryFile(strBase64, strFileName)
With CreateObject("ADODB.Stream")
.Type = 1 ' adTypeBinary
.Open
.Write Base64Decode(strBase64) ' Write the byte array
.SaveToFile strFileName, 2 ' Overwrite if file exists (adSaveCreateOverWrite)
End With
End Sub
Function Base64Decode(ByVal strText) ' As ByteArray
With CreateObject("MSXML2.DOMDocument.3.0").CreateElement("Base64")
.DataType = "bin.base64"
.Text = strText
Base64Decode = .NodeTypedValue
End With
End Function
So, back to your original question, how do you embed a binary (ICO) file in your VBScript file? You can just add the Base64 string somewhere. Put it at the end, the beginning, somewhere in the middle. But it'll need to be commented out, of course, since it's not valid VBScript. And you may want to add a start and end delimiter so you know where it begins and ends. For example:
' Read ourself...
With CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.ScriptFullName)
' Look for the "start"...
Do Until .AtEndOfStream
strLine = .ReadLine()
If strLine = "' ~END~" Then fRead = False
If fRead Then strBase64 = strBase64 & Mid(strLine, 3)
If strLine = "' ~START~" Then fRead = True
Loop
End With
' Re-create our bitmap!
Base64ToBinaryFile strBase64, "c:\stack_overflow.bmp"
' ~START~
' Qk1SAgAAAAAAADYAAAAoAAAADAAAAA8AAAABABgAAAAAABwCAAAAAAAAAAAAAAAAAAAAAAAA
' hoOChoOChoOChoOChoOChoOChoOChoOChoOChoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPl
' 3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlhoOChoOChoOChoOChoOChoOC3uPlhoOC3uPl3uPl
' hoOC3uPl3uPl3uPl3uPl3uPl3uPl3uPl3uPlhoOC3uPl3uPlhoOC3uPlcIyocIyocIyocIyo
' cIyocIyo3uPlhoOC3uPl3uPlhoOC3uPl3uPl3uPl3uPl3uPlwtTdn8DV3uPlhoOC3uPl3uPl
' 3uPl3uPl2uHknL/UcafJVpfCVJbCbKPI3uPl3uPl3uPl3uPl3+Tm3+TmVZfCXJvEeKvLr8na
' 3+Tmbq7cVKHZ3+Tm3+Tm3+Tm4eXn4eXn3ePm4eXn4eXnsM3iP5fYQZjXs8/jV6Tx097o4eXn
' 4ubo4ubo4ubo3uToY6jbN5PXdbLc3eToOJb0K4/0e63vdqrw4+fp4+fp4+fpQZjYRZvYwNbl
' 3uXpOJb0LZD00t7qMYP1lLvu5Ojq5Ojq5OjqxNjm5Ojq3+bqOpf0LpH01uHrcafwPInz5Ojq
' 5enr5enr5enr5enr5enrRJzzLpH01+Lr3OTrMIP1psbu5enr5+rs5+rs5+rs5+rs5+rsrs7u
' 3eXs5+rsZqLyQ4705+rs5+rs6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6Ovt6OvtN4f0s83v6Ovt6Ovt
' ~END~
On
There might be an easier method using the Windows build-in CERTUTIL command:
- Encode the ICON (or other binary) file with CERTUTIL (CERTUTIL -encode icon.ico icon.b64)
- Add the Base64 code in the script including
'prefix (REM) Use the next code to remove the REM and decode the Base64 code into a binary:
dim fso : set fso=CreateObject("scripting.FileSystemObject") dim wsh : set wsh=CreateObject("wscript.shell") '--- Extract ICO file... iconFile=fso.GetSpecialFolder(2) & "\icon" set f=fso.OpenTextFile(WScript.ScriptFullName) s=replace(f.ReadAll,"' ","") f.close set f=fso.OpenTextFile(iconFile & ".tmp",2,TRUE) f.writeline(s) f.close wsh.run "certutil -decode " & iconFile & ".tmp" & " " & iconFile & ".ico",0,true ' --- This is the output of the CERTUTIL encode command: ' -----BEGIN CERTIFICATE----- ' AAABAAYAEBAAAAAACABoBQAAZgAAACAgAAAAAAgAqAgAAM4FAAAwMAAAAAAIAKgO ' AAB2DgAAEBAAAAAAIABoBAAAHh0AACAgAAAAACAAqBAAAIYhAAAwMAAAAAAgAKgl ' .. ' .. ' AAAAHwAA/AAAAAA/AAD+AAAAAH8AAP+AAAAA/wAA/8AAAAP/AAD/4AAAB/8AAP/4 ' AAAf/wAA//4AAH//AAD//8AD//8AAA== ' -----END CERTIFICATE-----
This HTA show you how to embed a file exe or com zipped in a vbscript
For exemple i embeded the WGET.EXE to download a PNG file = estabanner5.png just for the test
[HTA] Encapsulate a zipped exe file in a VBScript
And i found this Vbscript on the net named Basic Base64- Encode- Decode.vbs You just Drop a file onto script and click YES to encode. And Click NO to decode a Base64 string.