Script Tips

InputBox() Alternatives

Don't sweat the technique--two ways to accept input from the command-line outside of the norm.

A user in the ScriptingAnswers.com discussion forums recently asked how to accept input from the command-line, rather than using InputBox(), which is VBScript’s only native function for accepting text input. Since this is such a common need in scripts, I thought I’d share a couple of techniques here.

Here’s how you’d prompt someone to enter a computer name, and then collect that name into a variable:

Dim strComputer
WScript.Echo "Enter computer name:"
strComputer = WScript.StdIn.ReadLine

If you’re just after one character, you could use:

Dim strResponse
WScript.Echo "(Y) for Yes or (N) for No:"
strResponse = WScript.StdIn.Read(1)

A downside to the above technique is that, while it’ll only return one character, it’ll allow the user to type as much as they want, and it won’t actually return anything to your script until the user hits Enter. There’s no simple, straightforward way of just collecting a single keystroke of information without the need to hit Enter.

Of course, these will only work if your script is running under CScript.exe (not WScript.exe); run CScript.exe //H:CScript from a command-line to make CScript the default for executing VBScripts on your system. Because the WScript object (which these two snippets utilize) is always available within Windows Script Host, there’s no need to use CreateObject() to explicitly instantiate an instance of the object.

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

comments powered by Disqus
Most   Popular