2014-09-28 52 views
0

我們如何使用msscript.ocx以編程方式知道synatx錯誤?如何使用msscript.ocx以編程方式知道synatx錯誤?

我用c#實現了msscript.ocx,它適用於vbscript。

考慮以下VBScript代碼:

For i=0 To 5 

'The following line has missing 'Then'. It should show an error. 
If i =2 
Exit For 
End If 

Next 

我們怎樣才能知道是否有含線錯誤「IF」,而不運行腳本?

+0

通過[下面的鏈接](http://stackoverflow.com/a/25983099/2165759),您可以找到C#代碼來檢查VBScript代碼是否有語法錯誤,它使用Microsoft Script Control。 – omegastripes 2014-10-23 18:40:09

回答

1

加載時出現錯誤。

Set Arg = WScript.Arguments 
set WshShell = createObject("Wscript.Shell") 
Set Inp = WScript.Stdin 
Set Outp = Wscript.Stdout 

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 

了Vbs

filter vbs "text of a vbs script" 

使用冒號將單獨的語句和線條。如果您需要單引號,請使用單引號代替雙引號(39)。用^字符轉義括號和&符號。如果你需要使用字母(136)。

該函數被稱爲UF(用於UserFunction)。它有兩個參數,包含當前行的L和包含行數的LC。將腳本的結果設置爲UF。看例子。

有三個全局對象可用。未聲明的全局變量gU維護狀態。如果您需要多個變量,請將其用作數組。用於保存和訪問先前行的Dictionary對象gdU。並準備使用RegExp對象。

示例 此vbs腳本插入行號並將行設置爲Filter打印的函數UF。

filter vbs "uf=LC ^& ' ' ^& L"<"%systemroot%\win.ini" 

這是它的外觀在內存

Dim gU 
Set gdU = CreateObject("Scripting.Dictionary") 
Set greU = New RegExp 

Function UF(L, LC) 

---from command line--- 
uf=LC & " " & L 
---end from command line--- 

End Function 

如果有語法錯誤過濾器會顯示調試信息。

相關問題