8

Very similar to this question:

How can I start an interactive console for Perl?

I just want to be able to start entering VBS statements, one at a time, and have them evaluated straight away, like Python's IDLE.

Community
  • 1
  • 1
user814116
  • 81
  • 1
  • 4

2 Answers2

10

I wrote this a couple years ago. It's based on this blog post (archived here), but with a couple enhancements. Essentially it's a REPL (Read, Execute, Print, Loop) using the Execute statement:

If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
    For Each Arg In WScript.Arguments
        If InStr( Arg, " " ) Then Arg = """" & Arg & """"
        Str = Str & " " & Arg
    Next
    WshShell.Run "cscript """ & WScript.ScriptFullName & """" & Str, 1
    WScript.Quit
End If

Do While True
    WScript.StdOut.Write(">>> ")

    line = Trim(WScript.StdIn.ReadLine)

    If LCase(line) = "exit" Then Exit Do

    On Error Resume Next
    Execute line
    If Err.Number <> 0 Then
        WScript.StdErr.WriteLine Err.Description
    End If
    On Error Goto 0
Loop

I usually start it with a batch file of the same name (i.e. "vbs.vbs" and "vbs.bat"), like this:

@cscript.exe //NoLogo %~dpn0.vbs
anishsane
  • 20,270
  • 5
  • 40
  • 73
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I'd add `If Left(line, 2) = "? " Then line = Replace(line, "? ", "WScript.Echo ", 1, 1)`, to mimic what the VBA IDE does. – Tomalak Sep 12 '18 at 10:30
  • 1
    Thanks for catching the syntax error. The code sample is just a minimalistic core implementation to demonstrate how the thing works in general. My real code (link at the beginning of my answer) is a little more elaborate and uses `?` for opening the VBScript help file at the given keyword. – Ansgar Wiechers Sep 12 '18 at 11:04
  • I wished it continued lines with "_". – js2010 Aug 13 '19 at 19:05
  • @js2010 The real thing does. – Ansgar Wiechers Aug 13 '19 at 22:08
  • This looks great for one-liners. I'm unable to create Subs and Functions. Is that a limitation of this REPL? I really like this answer, especially with @Tomalak 's extra. – Daniel 'Dang' Griffith Nov 03 '20 at 12:45
  • 1
    @Daniel'Dang'Griffith You can separate multiple statements with `:`, like `Sub Test() : Msgbox("Hey") : End Sub` but of course this gets unwieldy quickly. For more complex behaviour you'd have to move away from `WScript.StdIn.ReadLine()`, for example to a loop over `WScript.StdIn.Read(1)` that keeps reading characters until it finds a special "now run all this" keyword. Certainly doable if you put your mind to it. – Tomalak Nov 03 '20 at 13:56
0

You may try to make a debugger (cscript //X your.vbs) work for you, or to start a project of your own - perhaps based on these (first 3?) proposals

amloessb
  • 63
  • 1
  • 9
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96