Do you have an idea how can one vbs file verify the correctness of another vbs file but without executing it?
By "verify correctness" I mean that this second file can be compiled without getting an error.
Update:
As intersum pointed out, I did not realize that the WScript.Quit
will terminate the script that execute(global)s it. So using a shell can't be avoided.
Proof of concept script:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim goWS : Set goWS = CreateObject("WScript.Shell")
Dim aFiles : aFiles = Array("good.vbs", "bad.vbs")
Dim sFile
For Each sFile In aFiles
WScript.Echo sFile, "==>", checkSyntax(sFile)
Next
Function checkSyntax(sFile)
Dim sCode : sCode = goFS.OpenTextFile(sFile).ReadAll()
WScript.StdOut.Write " A " & sCode
sCode = "Option Explicit : WScript.Quit 0 : " & sCode
goFS.CreateTextFile("sctmp.vbs", True).WriteLine sCode
WScript.StdOut.Write " B " & goFS.OpenTextFile("sctmp.vbs").ReadAll()
Dim oExec : Set oExec = goWS.Exec("cscript sctmp.vbs")
Dim sOtp : sOtp = oExec.Stderr.ReadAll()
If "" = sOtp Then
checkSyntax = "ok"
Else
checkSyntax = sOtp
End If
End Function
output:
cscript sc.vbs
A WScript.Echo "good"
B Option Explicit : WScript.Quit 0 : WScript.Echo "good"
good.vbs ==> ok
A WScript.Echo "bad" : SomeSub(1, 2, 3)
B Option Explicit : WScript.Quit 0 : WScript.Echo "bad" : SomeSub(1, 2, 3)
bad.vbs ==> M:\lib\kurs0705\xpl\sctmp.vbs(1, 73) Microsoft VBScript compilation error: Cannot use parentheses
when calling a Sub
Update II:
As can be seen from:
type bad.vbs
WScript.Echo "bad" : SomeSub 1, 2, 3
cscript bad.vbs
bad
M:\lib\kurs0705\xpl\bad.vbs(1, 22) Microsoft VBScript runtime error: Type mismatch: 'SomeSub'
a runtime error may occur after most of the script has executed (output of "bad"). To deal with such errors, you must
None of these requirements are 'easy' in VBScript.