Skip to content

How to Reverse an Array in Batch Script

Learn how to efficiently reverse an array in Windows Batch using a symmetrical swap algorithm with two pointers, including implementation, use cases, and best practices.

In data processing, you often need to invert a sequence, such as reversing a breadcrumb trail to find your way back, inverting a list of priorities, or displaying the most recent entries of a stack at the bottom. Array Reversal (turning a list "upside down") is a straightforward task that can be automated by swapping elements from both ends of the list simultaneously.

In this guide, we will demonstrate how to reverse an array using a single loop and two pointers.

The Strategy: The Symmetrical Swap

To reverse an array of size N:

  1. Set a left pointer to 1.
  2. Set a right pointer to N.
  3. While left < right:
    • Swap the values at ARR[left] and ARR[right].
    • Increment left and decrement right.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Define the Array
set "size=5"
set "ARR_1=A"
set "ARR_2=B"
set "ARR_3=C"
set "ARR_4=D"
set "ARR_5=E"

echo Original: !ARR_1! !ARR_2! !ARR_3! !ARR_4! !ARR_5!

:: 2. Reversal Algorithm
set "left=1"
set "right=!size!"

:reverse_loop
if !left! GEQ !right! goto :done

:: Swap values using for-loop variable conversion
:: to resolve double-dynamic variable names
for %%L in (!left!) do for %%R in (!right!) do (
    set "temp=!ARR_%%L!"
    set "ARR_%%L=!ARR_%%R!"
    set "ARR_%%R=!temp!"
)

:: Move pointers inward
set /a "left+=1"
set /a "right-=1"

goto :reverse_loop

:done
echo Reversed: !ARR_1! !ARR_2! !ARR_3! !ARR_4! !ARR_5!

endlocal
pause

Output:

Original: A B C D E
Reversed: E D C B A

Why Reverse an Array?

  1. LIFO Logic: Converting a First-In-First-Out (FIFO) list into a Last-In-First-Out (LIFO) list for a different processing stage.
  2. Breadcrumb Trails: If your script logs every step it takes (A -> B -> C), reversing the list allows you to automatically generate an "Undo" or "Rollback" script (C -> B -> A).
  3. UI Display: Reversing a chronological list so that the newest data appears first for the user.

Important Considerations

  1. Midpoint: If the array has an odd number of elements (like 5), the middle element (C) stays exactly where it is. The logic if left GEQ right handles this automatically.
  2. Performance: This algorithm is very efficient (N/2 operations). It is the fastest way to reverse an array in memory.
  3. Variable Scope: If you are reversing data to be used in another window or by the user after the script ends, remember that setlocal will destroy your variables. Use the tunneling technique described above to save the results.

Best Practices

  1. Clear Memory: If you are done with the array, loop through the indices and unset each ARR_n variable explicitly to free up the environment space.
  2. Pointer Reset: If you perform multiple reversals in one script, ensure you reset your left and right pointers every time before entering the loop.

Conclusion

Reversing an array is a simple but essential data transformation. By mastering the symmetrical swap technique, you can easily invert the logic and sequence of your data sets, providing flexibility for "Rollbacks," "Undo" histories, and dynamic user interfaces. This control over data orientation ensures that your scripts are always ready for the next processing step, regardless of the original order of the information.

Navigation