Get user input
You'll often need to get some soft of input from user that you can process and perhaps return some data back to user.To syntax to get user input is 'SET /p yourName=[PROMPT MESSAGE]'
Type that in CMD and press enter, then type your name in and press enter again. Now as you'd expect, your name's been stored in 'yourName' vairable, now you can retrieve it.
To see that name user enter. echo the variable 'yourName', note that variable name must between % percent sign, like %variableName%.
Alright, lets ask user for his name and tell him nice to meet you with his name at the end.
Here's how we'd go about it.
1. First off lets disable commands from appearing on screen using command '@alerts off'
2. Prompt user to enter his name using 'SET /p yourName="Enter your username "'. The reason I've surrounded Enter your name with double quote is to add little bit of space after 'username' to when user enter his name there's space between 'username' and his name.
3. Write back to user 'Nice to meet you HIS NAME' by calling the variable 'yourName'
4. Pause so it doesn't exit
@echo off
SET /p name="Please enter your name "
echo Nice to meet you %name%
pause

No comments:
Post a Comment