How to Merge Two Arrays into One Array in Batch Script
Combining data from different sources, such as a list of servers from one file and a list of workstations from another, is a common task in system automation. Array Merging involves taking the contents of two separate lists and appending them into a single, master array. This allows you to perform a single loop over all your targets instead of managing multiple separate processes.
In this guide, we will demonstrate how to merge two arrays using a sequential FOR /L loop.
The Strategy: The Append Loop
The easiest way to merge arrays is:
- Read the first array and copy it to the
MASTERarray. - Read the second array and append its values to the
MASTERarray, starting exactly where the first one finished.
This method preserves the original order of both arrays. All items from Array A appear first, followed by all items from Array B, maintaining their internal sequence.
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: 1. Define Array A
set "sizeA=3"
set "A_1=Server-Alpha"
set "A_2=Server-Beta"
set "A_3=Server-Gamma"
:: 2. Define Array B
set "sizeB=2"
set "B_1=WS-01"
set "B_2=WS-02"
echo Array A: !A_1!, !A_2!, !A_3!
echo Array B: !B_1!, !B_2!
echo.
echo Merging Array A and Array B...
:: 3. Merge Logic
set "mSize=0"
:: Copy Array A into MASTER
for /L %%i in (1,1,%sizeA%) do (
call set "val=%%A_%%i%%"
if defined val (
set /a "mSize+=1"
set "MASTER_!mSize!=!val!"
)
)
:: Append Array B into MASTER
for /L %%i in (1,1,%sizeB%) do (
call set "val=%%B_%%i%%"
if defined val (
set /a "mSize+=1"
set "MASTER_!mSize!=!val!"
)
)
:: 4. Display Results
echo.
echo --- MASTER ARRAY (!mSize! items^) ---
for /L %%i in (1,1,!mSize!) do (
echo [%%i] !MASTER_%%i!
)
endlocal
pause
Output:
Array A: Server-Alpha, Server-Beta, Server-Gamma
Array B: WS-01, WS-02
Merging Array A and Array B...
--- MASTER ARRAY (5 items) ---
[1] Server-Alpha
[2] Server-Beta
[3] Server-Gamma
[4] WS-01
[5] WS-02
Press any key to continue . . .
Why Merge Arrays?
- Unified Processing: If you have 5 different scripts gathering error codes, you can merge their results into one array to generate a single, consolidated "Global Error Report."
- Batch Actions: Gathering a list of "Files to Delete" and "Folders to Delete" into one master "Cleanup Queue."
- Data Synchronization: Combining an "Old Registry" and a "New Registry" before performing a deduplication check to see what has changed.
Important Considerations
Always use a unique prefix for your master array (like MASTER_) that differs from your source array prefixes (A_, B_). If the prefixes overlap, appending values will silently overwrite the source data, causing data loss and incorrect merge results.
- Name Collisions: Ensure your master array uses a unique prefix (like
MASTER_) so it doesn't accidentally overwrite your source arrays if they happen to share variable names. - Indexing: Always track your
mSize(Master Size) carefully. If you reset the counter by accident, you will overwrite your previous data. - Memory Limits: While Batch can handle thousands of variables, merging very large arrays (e.g., 10,000+ items each) can consume significant environment memory. For massive datasets, consider merging text files using
type file2.txt >> file1.txtinstead of using memory-based arrays.
Best Practices
- Skip Empty Elements: Before adding an item to the master array, verify it is defined. This prevents "Empty slots" in your final list. The implementation above includes this guard with
if defined val. - Deduplication: Often, after merging two arrays, you will find duplicate entries. It is best practice to run a Deduplication routine immediately after the merge to keep your master list clean.
To merge more than two arrays, simply repeat the append loop pattern for each additional source array (C, D, etc.). The mSize counter continues incrementing from where the previous loop left off, so all items are appended sequentially without any index conflicts.
Conclusion
Merging arrays is a vital "Consolidation" technique that simplifies complex data management. By turning multiple fragmented lists into a single, unified inventory, you reduce the complexity of your loops and ensure that your automation processes all gathered data in a consistent, orderly fashion. This ability to combine datasets is essential for building professional, large-scale reporting and maintenance tools.