How to Take String Input in Batch Script
Sometimes we need to deal with various string inputs such as first name, last name, address, etc. We take this input for various purposes.
This article will show how we can declare a string variable and assign it to the value from the user input. Also, we will see some examples with explanations regarding this topic for better understanding.
Take String Input in Batch Script
The format to declare a string variable and assign its value from user input is similar to the format we generally follow to declare variables. The general format to declare string variables with values from user input is the following.
SET /P stringVariable = Instructional string
Here, Instructional string
is a message to the user that instructs the user about the required input for the program. Let’s see an example with explanations for better understanding.
@echo off
SET /P YourName=Enter your name:
ECHO Your name is %YourName%
Let’s discuss the code shared above briefly.
On the line SET /P YourName=Enter your name:
, we declared a value named YourName
that’s value will assign by user input. Notice that we provided an instructional message Enter your name:
to the user so that the user can understand that he needs to input his name in this field.
We showed the user name on our last line using ECHO Your name is %YourName%
. After successful compilation, the output of the program will look like this.
Output:
Enter your name: Alen
Your name is Alen
Remember, all methods discussed here are written using Batch Script and will only work in a Windows CMD environment.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn