SAPIEN Solutions

SAPIEN homepage
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

Running command-line tools

 These examples show how to run command-line tools from within a VBScript, capturing the tool’s output.

  1. '
  2. ' ScriptingAnswers.com Essentials - by Don Jones
  3. '
  4. ' Run cmdline tools
  5. ' This shows how to launch a command-line
  6. ' tool and retrieve its output into a string variable
  7. ' to then do something with it. For example, you might
  8. ' use the InStr() function to determine if a specific
  9. ' string exists in the command's output.

  10. ' The trick is to use the WshShell object's Exec, not Run, method
  11. Dim objShell, objExec
  12. Set objShell = CreateObject("WScript.Shell")

  13. 'run the command-line command - just as you'd type it into
  14. 'Start > Run. Avoid running Cmd.Exe directly, because it
  15. 'doesn't actually return any output itself.
  16. Set objExec = objShell.Exec("dir")

  17. 'objExec now represents the executing command. Our script
  18. 'doesn't exactly wait for that to finish, but we can
  19. 'keep in touch with it. objExec is an instance of the
  20. 'WshScriptExec object, if you want to read the docs on it

  21. 'we can wait for it to finish running
  22. Do Until objExec.Status = 1
  23. WScript.Sleep 100
  24. Loop

  25. 'we can get the exit code (note that this method
  26. 'isn't in the WSH Type Library so it doesn't show up
  27. 'in a lot of object browsers or code hinting menus,
  28. 'but it should work on the latest version of WSH)
  29. WScript.Echo "Exited with " & objExec.ExitCode

  30. 'here's where we get the output into a string var
  31. Dim strOutput
  32. If Not objExec.StdOut.AtEndOfStream Then
  33. strOutput = strOutput & objExec.StdOut.Read(1)
  34. End If
  35. WScript.Echo strOutput
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: , , , , , ,

Leave a Reply


Entries (RSS) and Comments (RSS).