Sunday 23 June 2019

Batch: Read input from user


‘set /p variable=[prompt string]’ used to read input from user. It prompts the user with given prompt string and assign the user entered value to the variable.

Hello.bat
@echo off

set /p firstName=Enter first name 
set /p lastName=Enter last name 

echo Hello Mr %firstName% %lastName%

C:\Users\Public>Hello
Enter first name Krishna
Enter last name Gurram
Hello Mr Krishna Gurram

You can rewrite the above application using string concatenation like below.


Hello.bat
@echo off

set /p firstName=Enter first name 
set /p lastName=Enter last name 

set name=%firstName% %lastName%

echo Hello Mr %name%

Output
C:\Users\Public>Hello
Enter first name Krishna
Enter last name Gurram
Hello Mr Krishna Gurram


Previous                                                 Next                                                 Home

No comments:

Post a Comment