How to turn vbs and js files into exe files from the command line
Any COM Automation programming language (that's all of them but especially VB6 which is what this object was designed for - it also applies to vbscript too).
Use the MS Script Control.
For those that don't have the great VB6 (a member of the same family as vbscript) here's how to make an exe with a script from the command line and notepad.
Create text file on desktop and name it VB2Exe.vb
The Scrpt string variable contains your script.
<code>
Public Module MyApplication
Public Sub Main()
Dim sc as object
Dim Scrpt as string
sc = createObject("MSScriptControl.ScriptControl")
Scrpt = "msgbox " & chr(34) & "Hi there" & chr(34)
With SC
.Language = "VBScript"
.UseSafeSubset = False
.AllowUI = True
End With
sc.addcode(Scrpt)
End Sub
End Module
</code>
Looks fairly familar.
The .NET framework comes with compilers.
You need to make sure the path to the compiler is correct on your windows install.
In an elevated command prompt type
<code>
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /t:exe "%userprofile%\desktop\VB2Exe.vb"
</code>
Your first VBScript in an exe.
Here's sample vbs code that syntax checks scripts. But the global code does run in the script control. (even though I'm using executeglobal to run it for real).
It runs the vbs script specified against every line in a textstream (Inp) writing it to another textstream (Outp).
Ag(1) is the script to be added. I add extra script to the users script.
You need to define Inp, Outp, and Ag(1).
Note the syntax error checking and messages to the user.
Sub VBSCmd
RawScript = Arg(1)
'Remove ^ from quoting command line and replace : with vbcrlf so get line number if error
Script = Replace(RawScript, "^", "")
Script = Replace(Script, "'", chr(34))
Script = Replace(Script, ":", vbcrlf)
'Building the script with predefined statements and the user's code
Script = "Dim gU" & vbcrlf & "Dim gdU" & vbcrlf & "Set gdU = CreateObject(" & chr(34) & "Scripting.Dictionary" & chr(34) & ")" & vbcrlf & "Function UF(L, LC)" & vbcrlf & "Set greU = New RegExp" & vbcrlf & "On Error Resume Next" & vbcrlf & Script & vbcrlf & "End Function" & vbcrlf
'Testing the script for syntax errors
On Error Resume Next
set ScriptControl1 = wscript.createObject("MSScriptControl.ScriptControl",SC)
With ScriptControl1
.Language = "VBScript"
.UseSafeSubset = False
.AllowUI = True
.AddCode Script
End With
With ScriptControl1.Error
If .number <> 0 then
Outp.WriteBlankLines(1)
Outp.WriteLine "User function syntax error"
Outp.WriteLine "=========================="
Outp.WriteBlankLines(1)
Outp.Write NumberScript(Script)
Outp.WriteBlankLines(2)
Outp.WriteLine "Error " & .number & " " & .description
Outp.WriteLine "Line " & .line & " " & "Col " & .column
Exit Sub
End If
End With
ExecuteGlobal(Script)
'Remove the first line as the parameters are the first line
'Line=Inp.readline
Do Until Inp.AtEndOfStream
Line=Inp.readline
LineCount = Inp.Line
temp = UF(Line, LineCount)
If err.number <> 0 then
outp.writeline ""
outp.writeline ""
outp.writeline "User function runtime error"
outp.writeline "==========================="
Outp.WriteBlankLines(1)
Outp.Write NumberScript(Script)
Outp.WriteBlankLines(2)
Outp.WriteLine "Error " & err.number & " " & err.description
Outp.WriteLine "Source " & err.source
Outp.WriteLine "Line number and column not available for runtime errors"
wscript.quit
End If
outp.writeline temp
Loop
End Sub
Function NumberScript(Text)
Text = Replace(Text, vbcr, "")
LineArray=Split(Text,vblf)
For i = 0 to UBound(LineArray, 1) - 1
Count = Count + 1
Temp = Temp & vbcrlf & Count & " " & LineArray(i)
Next
NumberScript = Temp
End Function
.
--