Windows Vista Forums

How to read a user input value from command line and store it in a variable?
  1. #1


    Tony Bansten Guest

    How to read a user input value from command line and store it in a variable?

    I would like to read a user input from command line and store it in a variable aaa.



    In good ol' DOS batch scripts there is a command:

    set /p aaa="Please enter value: "

    How do I do this in VBscript?

    Is there a way to define a default value? In other words if the user simply hits RETURN
    the value in variable aaa should not be empty but contain instead e.g. "unknown" or 0 (=zero)

    How do I define such a default value in VBscript?

    Again: the input should not be entered in a GUI based entry field but at the command prompt.

    Tony


      My System SpecsSystem Spec

  2. #2


    Pegasus [MVP] Guest

    Re: How to read a user input value from command line and store it in a variable?


    "Tony Bansten" <tonytony@xxxxxx> wrote in message
    news:49c9f0be$0$31862$9b4e6d93@xxxxxx-online.net...

    >I would like to read a user input from command line and store it in a
    >variable aaa.
    >
    > In good ol' DOS batch scripts there is a command:
    >
    > set /p aaa="Please enter value: "
    >
    > How do I do this in VBscript?
    >
    > Is there a way to define a default value? In other words if the user
    > simply hits RETURN
    > the value in variable aaa should not be empty but contain instead e.g.
    > "unknown" or 0 (=zero)
    >
    > How do I define such a default value in VBscript?
    >
    > Again: the input should not be entered in a GUI based entry field but at
    > the command prompt.
    >
    > Tony
    Strange . . . for a long time people have been complaining about the
    unfriendly black-screen user input method offered by set /p, and now you
    wish to use precisely this unfriendly technique . . . AFAIK, VB Script has
    no such facility. Its input tool is, as you probably know, the InputBox
    method.

    A rather clumsy way to meet your requirement would be to use a batch file
    that does two things:
    - It uses set /p to prompt the user for an input.
    - It writes the response to a text file.

    Your VB Script will also do two things:
    - If invokes the batch file like so:
    oWshShell.Run "d:\temp\test.bat", 3, True
    - It reads the input from the text file.

    I think the InputBox method would be a lot more elegant.



      My System SpecsSystem Spec

  3. #3


    Clive Lumb Guest

    Re: How to read a user input value from command line and store it in a variable?


    "Tony Bansten" <tonytony@xxxxxx> a écrit dans le message de news:
    49c9f0be$0$31862$9b4e6d93@xxxxxx-online.net...

    >I would like to read a user input from command line and store it in a
    >variable aaa.
    >
    > In good ol' DOS batch scripts there is a command:
    >
    > set /p aaa="Please enter value: "
    >
    > How do I do this in VBscript?
    >
    > Is there a way to define a default value? In other words if the user
    > simply hits RETURN
    > the value in variable aaa should not be empty but contain instead e.g.
    > "unknown" or 0 (=zero)
    >
    > How do I define such a default value in VBscript?
    >
    > Again: the input should not be entered in a GUI based entry field but at
    > the command prompt.
    >
    I hope that we are not doing your homework for you

    Run this with the cscript shell

    Dim StdIn, StdOut
    Set StdIn = WScript.StdIn
    Set StdOut = WScript.StdOut
    StdOut.WriteLine "Please enter value"
    str = StdIn.ReadLine
    if len(str)=0 then str="0"



      My System SpecsSystem Spec

  4. #4


    Armin Zingler Guest

    Re: How to read a user input value from command line and store it in a variable?

    "Tony Bansten" <tonytony@xxxxxx> schrieb

    >
    > How do I do this in VBscript?
    Is there a reason why VBscript questions are also sent to the VB.Net group
    lately? I'd just like to know. They have little in common - saying this
    without really knowing VBscript.


    Armin


      My System SpecsSystem Spec

  5. #5


    Mr. Taco Guest

    Re: How to read a user input value from command line and store itin a variable?

    Check out the console application at this link:
    http://msdn.microsoft.com/en-us/bb964686.aspx

    It's under the "Application Samples" folder.

    It's a good example of how to read and write to the console.

    -Taco

    Tony Bansten wrote:

    > I would like to read a user input from command line and store it in a variable aaa.
    >
    > In good ol' DOS batch scripts there is a command:
    >
    > set /p aaa="Please enter value: "
    >
    > How do I do this in VBscript?
    >
    > Is there a way to define a default value? In other words if the user simply hits RETURN
    > the value in variable aaa should not be empty but contain instead e.g. "unknown" or 0 (=zero)
    >
    > How do I define such a default value in VBscript?
    >
    > Again: the input should not be entered in a GUI based entry field but at the command prompt.
    >
    > Tony
    >

      My System SpecsSystem Spec

  6. #6


    Clive Lumb Guest

    Re: How to read a user input value from command line and store it in a variable?


    "Tony Bansten" <tonytony@xxxxxx> a écrit dans le message de news:
    49c9f0be$0$31862$9b4e6d93@xxxxxx-online.net...

    >I would like to read a user input from command line and store it in a
    >variable aaa.
    >
    > In good ol' DOS batch scripts there is a command:
    >
    > set /p aaa="Please enter value: "
    >
    > How do I do this in VBscript?
    >
    > Is there a way to define a default value? In other words if the user
    > simply hits RETURN
    > the value in variable aaa should not be empty but contain instead e.g.
    > "unknown" or 0 (=zero)
    >
    > How do I define such a default value in VBscript?
    >
    > Again: the input should not be entered in a GUI based entry field but at
    > the command prompt.
    >
    > Tony
    >
    I hope that we are not doing your homework for you

    Run this with the cscript shell

    Dim StdIn, StdOut
    Set StdIn = WScript.StdIn
    Set StdOut = WScript.StdOut
    StdOut.WriteLine "Please enter value"
    str = StdIn.ReadLine
    if len(str)=0 then str="0"



      My System SpecsSystem Spec

How to read a user input value from command line and store it in a variable? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to display a variable value in a dialog box and how to read user input? Richard Mueller [MVP] VB Script 0 20 Jun 2008
How take an user input in command line Options Developer PowerShell 1 26 Apr 2007
How to read CSV to another command's input? james PowerShell 4 14 Mar 2007
Read-Host issue, won't store to variable when using multiple read-host lines bryan.rutkowski@gmail.com PowerShell 7 09 Feb 2007
Command Line Input? Singee PowerShell 1 31 May 2006