How to Concatenate (Join) Two or More Strings in Batch Script
String concatenation, i.e. the process of joining multiple strings together to form a single string, is one of the most fundamental operations in scripting. You need it for building dynamic file paths, creating formatted output messages, or constructing commands to be executed. In batch scripting, this is achieved through simple variable expansion, placing variables and literal text next to each other.
This guide will teach you the basic syntax for joining strings, how to correctly handle spaces and special characters, and the critical technique of using delayed expansion for concatenating inside a loop.
The Core Method: Simple Variable Expansion
The most direct way to concatenate strings in a batch script is to simply place the variables right next to each other. The command processor will expand each variable to its value before executing the command, effectively joining them together.
Syntax: ECHO %String1%%String2%
or an alternative syntax: SET "NewString=%String1%%String2%"
Basic Example: Creating a Full Name
Let's start with two variables, a first name and a last name, and join them.
@ECHO OFF
SET "FirstName=John"
SET "LastName=Doe"
ECHO %FirstName% %LastName%
We explicitly add a space between the variables to ensure the output is correctly formatted.
Output:
John Doe
Concatenating with Literal Text
You are not limited to joining only variables. You can easily mix variables with hardcoded (literal) text to build more complex strings.
@ECHO OFF
SET "UserName=Alice"
SET "Status=Logged In"
ECHO User: %UserName% - Status: %Status%
Output:
User: Alice - Status: Logged In
Storing the Result in a New Variable
Often, you'll want to store the combined string in a new variable for later use. This is done with the SET command.
@ECHO OFF
SET "FirstName=Jane"
SET "LastName=Smith"
REM Concatenate the two variables with a space in between.
SET "FullName=%FirstName% %LastName%"
ECHO The full name is: %FullName%
Output:
The full name is: Jane Smith
Common Pitfalls and How to Solve Them
Problem: No Space Between Variables
A common mistake is forgetting that spaces are not added automatically. The command processor joins the strings exactly as you type them.
Example of a script with the error:
@ECHO OFF
SET "Part1=Hello"
SET "Part2=World"
ECHO %Part1%%Part2%
Output:
HelloWorld
Solution: You must explicitly add any spaces or other separators you need.
ECHO %Part1% %Part2%
Problem: Handling Special Characters (&, |, >)
If one of your variables contains a special command character, it can break your concatenation command.
Example of a script with the error:
@ECHO OFF
SET "Var1=Salt"
SET "Var2=& Pepper"
REM This will FAIL. The & is interpreted as a command separator.
SET Combined=%Var1% %Var2%
Output:
'Pepper' is not recognized as an internal or external command...
Solution: Quote the SET Assignment
The most robust way to perform a SET operation is to enclose the entire assignment in double quotes. This tells the command processor to treat everything after the = as a literal string.
REM This is the correct, safe syntax.
SET "Combined=%Var1% %Var2%"
ECHO %Combined%
Output:
Salt & Pepper
Problem: Concatenating Inside a Loop (Delayed Expansion)
This is the most advanced and critical pitfall. In a FOR loop, standard percent variables (%Var%) are expanded only once, before the loop starts. If you try to build a string inside the loop, you will only see the initial value.
Example of a script with the error:
@ECHO OFF
SET "MyList="
FOR %%A IN (A B C) DO (
SET "MyList=%MyList% %%A"
ECHO Inside loop: %MyList%
)
ECHO Final result: %MyList%
Output:
Inside loop: A
Inside loop: B
Inside loop: C
Final result: C
The variable is not accumulating as expected.
Solution: Use Delayed Expansion
You must enable delayed expansion and use exclamation marks (!Var!) to access the current value of the variable inside the loop.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MyList="
FOR %%A IN (A B C) DO (
SET "MyList=!MyList! %%A"
ECHO Inside loop: !MyList!
)
ECHO Final result: !MyList!
Output:
Inside loop: A
Inside loop: A B
Inside loop: A B C
Final result: A B C
Practical Example: Building a Dynamic File Path
This is one of the most common use cases for concatenation. The script builds a full path to a log file using the script's own directory, a base name, and the current date.
@ECHO OFF
SETLOCAL
REM --- Get the base path from the script's location ---
SET "LogDir=%~dp0Logs\"
REM --- Create a date stamp like YYYY-MM-DD ---
SET "DateStamp=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"
REM --- Concatenate the parts to build the full path ---
SET "LogFile=%LogDir%AppLog_%DateStamp%.log"
ECHO The log file for today will be created at:
ECHO "%LogFile%"
REM Ensure the log directory exists
MKDIR "%LogDir%" 2>NUL
ECHO %TIME% - Log file created. > "%LogFile%"
Conclusion
String concatenation is a simple but powerful feature in batch scripting that is essential for creating dynamic and flexible automation.
- The basic method is to simply place variables and text next to each other:
%Var1%_literal_text_%Var2%. - Always quote your
SETassignments (SET "MyVar=...") to safely handle special characters. - When building a string inside a
FORloop, you must use Delayed Expansion (SETLOCAL ENABLEDELAYEDEXPANSION) and exclamation marks (!MyVar!).
By mastering these techniques, you can reliably construct any string your script needs.