Open In App

Batch Script – String Concatenation

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

String Concatenation is combining two or more strings to create a new string. For instance, let a string “Good” and take another string “Morning” now by string concatenation i.e. “Good” + “Morning” and we got new string i.e. “Good Morning”. This is string concatenation.

Let see some examples of string concatenation using batch script.

Example 1 :

In this example, we concatenate two strings.

First, we create a batch file named “concatenation.bat” and open it in notepad.

@echo off
:: We take two string Good and Morning
set str1=Good
set str2=Morning
:: Below command will join two string and store it in str_new variable
set str_new=%str1% %str2%
echo %str_new%
pause

Save the above batch script and run it.

Note:  “pause” is used to hold the screen of the executed file until any key is pressed.

Output : 

Good Morning

Example 2 :

In this example, we concatenate more than two strings.

First, open the notepad and write the following command.

@echo off
:: Here we take three string
set str1=Hello Dear
set str2=Welcome to Geeks For Geeks site
set str3=This is example of string Concatenation
:: Below command will join these strings and store it in str_new variable
set str_new=%str1% %str2% and %str3%
echo %str_new%
pause

Save the above batch script with the .bat extension and run it.

Output : 

Hello Dear Welcome to Geeks For Geeks site and This is example of string Concatenation

Above in both examples, we see how to concatenate string using batch script.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads