Skip to main content

How to Get the Index of a Value in an Array in Batch Script

Simply knowing if a value exists in a list is often not enough, you often need to know where it is. Finding the Index (the position number) allows you to perform secondary actions, such as removing the item, replacing it, or accessing related data in a parallel array (e.g., finding a "Username" at Index 5 so you can look up their "Join Date" at Index 5 in a separate list).

In this guide, we will demonstrate how to find the index of a value using a FOR loop.

The Strategy: The Search Pointer

  1. Initialize a foundIndex variable to -1 (indicating "Not Found").
  2. Iterate through the array using a FOR /L loop.
  3. Compare the array value at the current index to your target.
  4. If it matches, store the current index and goto :done (exit the loop early).
note

This performs a linear search with an early exit. In the best case (item at position 1), it completes in a single comparison. In the worst case (item not found), it checks all N elements.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Define the Array
set "size=5"
set "ARR_1=SQL-01"
set "ARR_2=IIS-05"
set "ARR_3=APP-02"
set "ARR_4=SQL-99"
set "ARR_5=FS-01"

:: 2. Search Target
set "target=APP-02"
set "foundIndex=-1"

echo Searching for the position of "!target!"...

:: 3. The Search Loop
for /L %%i in (1,1,%size%) do (
call set "val=%%ARR_%%i%%"
if /i "!val!"=="!target!" (
set "foundIndex=%%i"
goto :show_result
)
)

:show_result
echo.
if !foundIndex! GEQ 0 (
echo [FOUND] "!target!" is located at Index: !foundIndex!
) else (
echo [ERROR] "!target!" was not found in the list.
)

endlocal
pause

Why Find an Array Index?

  1. Deletion: If a user wants to "Remove Item 3," you first need to find which value is currently at that index or find the index of a specific value so you can "Delete" it from your database.
  2. Parallel Lookups: If you have multiple arrays (e.g., Names, IPs, Departments), finding the index of "John Smith" in the Names array tells you that his IP will be at the same index in the IPs array.
  3. Replacement: Identifying exactly where a value is so you can update it (e.g., replacing "Old-Server" with "New-Server" in a configuration list).

Important Considerations

warning

If your array contains the same value multiple times, this script finds the first occurrence and stops. If you need the last occurrence, remove the goto :show_result line so the loop continues to the end, and foundIndex will hold the position of the final match.

  1. Duplicates: If your array contains the same value multiple times, this script will find the First occurrence and stop. If you need the Last occurrence, remove the goto :show_result so the loop continues to the end.
  2. Indexing Base: Most Batch arrays are 1-based (starting at 1). If you are using a 0-based array, ensure your FOR /L loop starts at 0.
  3. No Matches: Always initialize your index variable to a non-standard number like -1 or NONE. This allows your results logic to distinguish between "Index 0" and "Not Found."

Best Practices

  1. Case Sensitivity: Use if /i for case-insensitive matches to ensure that sql-02 matches SQL-02.
  2. Clear Exit: Using goto to exit the loop once the item is found is crucial for performance. In a 1,000-item array, if the item is at position 5, there is no reason to waste cycles checking the remaining 995 items.
tip

To find all occurrences of a value, remove the goto exit and collect each matching index into a results array: set /a "matchCount+=1" and set "MATCH_!matchCount!=%%i". After the loop, matchCount tells you how many matches were found and MATCH_1, MATCH_2, etc. hold their positions.

Conclusion

Finding the index of a value is a key building block for advanced list manipulation. By identifying the exact coordinate of a value within your data structure, you gain the ability to perform precise updates, deletions, and cross-references. This level of granular control is what allows you to build Batch scripts that manage complex, interrelated data systems with the precision of a professional desktop application.