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
- Initialize a
foundIndexvariable to -1 (indicating "Not Found"). - Iterate through the array using a
FOR /Lloop. - Compare the array value at the current index to your target.
- If it matches, store the current index and
goto :done(exit the loop early).
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?
- 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.
- Parallel Lookups: If you have multiple arrays (e.g.,
Names,IPs,Departments), finding the index of "John Smith" in theNamesarray tells you that his IP will be at the same index in theIPsarray. - 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
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.
- 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_resultso the loop continues to the end. - Indexing Base: Most Batch arrays are 1-based (starting at 1). If you are using a 0-based array, ensure your
FOR /Lloop starts at 0. - No Matches: Always initialize your index variable to a non-standard number like
-1orNONE. This allows your results logic to distinguish between "Index 0" and "Not Found."
Best Practices
- Case Sensitivity: Use
if /ifor case-insensitive matches to ensure thatsql-02matchesSQL-02. - Clear Exit: Using
gototo 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.
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.