VBScript interactive shell

One of the things I really love about Python is the interactive shell; when I think “how do I do X in Python?” then I can just drop to a shell, type “python“, and then try stuff out. I can’t do that in VBScript, and it annoys me. So: an interactive VBScript shell.

do while true
  wscript.stdout.write(">>> ")
  ln = wscript.stdin.readline
  if lcase(trim(ln)) = "exit" then exit do
  on error resume next
  err.clear
  execute ln
  if err.number <> 0 then wscript.echo(err.description)
  on error goto 0
loop

Note that this is totally daft and it doesn’t do anythng complex at all. No line continuations. No cleverness. But it helps me to answer the sorts of questions I come up with a lot, like “does a string with length > 0 evaluate to True?” without having to create a .vbs file and run it. You need to run this from the command line with cscript.

4 Responses to “VBScript interactive shell”

  1. Do you have a Fortran version? :)

    I agree with you, the interactive shell is one of the best things about Python. Everytime I’m coding in Fortran (really, I do that) I almost get crazy.

    Roberto
  2. Ha, great minds think alike!

    I wrote a GUI (WScript) version of this a while back.  Here’s the whole thing (hope it displays right):
    <pre><code>
    OPTION EXPLICIT

    DIM strCodeToExec
    strCodeToExec = “msgbox Now()”

    DO WHILE strCodeToExec<>”"
    strCodeToExec = InputBox(”Enter VBS code to execute”, “WSH Prompt”, strCodeToExec)
    IF strCodeToExec<>”" THEN
    on error resume next

    execute(strCodeToExec)

    IF err.Number<>0 THEN
    call msgbox(”Error #” & err.number & vbCRLF & err.description, vbExclamation, “Run Error”)
    END IF

    on error goto 0
    END IF
    LOOP
    </code></pre>

    It just prompts for code (with an inputbox), execs that code & then asks for more.  Quite handy.

    Btw, it’s downloadable as “WSHPrompt” at http://slingfive.com/pages/tools/.

    Rob Eberhardt, Slingshot Solutions
  3. How does the original script work?
    There’s no such object as “wscript“. How do you create such an object?

    I’d really love to see a working version of this.

    Thanks,
    Martin

    Martin
  4. Martin: put the code in a file called “vbs.vbs“, and then run it with “cscript //nologo vbs.vbs“.

    sil

Leave a Reply

OpenID is a decentralised authentication system. If you use LiveJournal or Vox you already have an OpenID; just use the URL of your homepage there. See also how to get yourself an OpenID.