Skip to content

How to Merge Two Arrays into One Array in Batch Script

Learn how to combine two arrays in a Windows batch script using sequential FOR /L loops while preserving order, with practical examples and best practices for automation.

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:

  1. Read the first array and copy it to the MASTER array.
  2. Read the second array and append its values to the MASTER array, starting exactly where the first one finished.

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?

  1. 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."
  2. Batch Actions: Gathering a list of "Files to Delete" and "Folders to Delete" into one master "Cleanup Queue."
  3. Data Synchronization: Combining an "Old Registry" and a "New Registry" before performing a deduplication check to see what has changed.

Important Considerations

  1. 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.
  2. Indexing: Always track your mSize (Master Size) carefully. If you reset the counter by accident, you will overwrite your previous data.
  3. 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.txt instead of using memory-based arrays.

Best Practices

  1. 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.
  2. 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.

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.

Navigation