1

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.

intersum
  • 596
  • 7
  • 19

1 Answers1

1
  1. .ReadAll() the file to check
  2. Remove "Option Explicit" if necessary, prepend "Option Explicit : WScript.Quit 0"
  3. On Error Resume Next : Execute[Global] (modified) source : Handle 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

  1. design and implement robust programs
  2. do intensive tests
  3. implement rutime error handling

None of these requirements are 'easy' in VBScript.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • @NickODell - that's why you prepend "WScript.Quit 0" – Ekkehard.Horner Nov 16 '12 at 17:40
  • @Ekkehard.Horner - I tried to do it this way but it didn't work. This **WScript.Quit 0** causes application close... Here is the simple code that doesn't work: **Option Explicit : On Error Resume Next : Dim someCode : Dim ABC : ABC = "Some text" : someCode = "WScript.Quit 0 : WScript.Echo ABC" : WScript.Echo "Start executing" : Execute someCode : WScript.Echo "Code executed" : If Err.Number <> 0 Then : WScript.Echo Err.Description : End If** – intersum Nov 21 '12 at 10:48
  • @Ekkehard.Horner - thank you. It works for syntax checking but when I put a random string (e.g. "__asdfgh__") in this **"bad.vbs"** file I got a result: **"bad.vbs ==> ok"** so it it doesn't account for all cases. Do you have any idea how to improve this? – intersum Nov 21 '12 at 13:03
  • @intersum a *compile* time check can't find *runtime* errors. (see update) – Ekkehard.Horner Nov 21 '12 at 13:09
  • @Ekkehard.Horner - I see, so is there any way to catch this _runtime_ errors? – intersum Nov 21 '12 at 13:26
  • @intersum - do some research on "On Error Resume Next" and look at http://stackoverflow.com/a/13481923/603855 – Ekkehard.Horner Nov 21 '12 at 13:33