0

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(missing ) を含む行にエラーがあるかどうかを確認するにはどうすればよいでしょうか?Then

4

1 に答える 1

2

読み込み時にエラーが発生します。

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"

コロンを使用してステートメントと行を区切ります。二重引用符の代わりに単一引用符を使用します。単一引用符が必要な場合は、chr(39) を使用します。括弧とアンパサンドを ^ 文字でエスケープします。キャレットが必要な場合は、chr(136) を使用します。

この関数は UF (UserFunction の略) と呼ばれます。現在の行を含む L と行数を含む LC の 2 つのパラメーターがあります。スクリプトの結果を UF に設定します。例を参照してください。

3 つのグローバル オブジェクトを使用できます。状態を維持する宣言されていないグローバル変数 gU。複数の変数が必要な場合は、配列として使用します。前の行を保存してアクセスするための Dictionary オブジェクト gdU。そして、すぐに使える RegExp オブジェクト grU。

例 次の 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

構文エラーがある場合、フィルターはデバッグの詳細を表示します。

于 2014-09-28T20:59:13.550 に答える