How to Perform a Linear Search in Batch Script
The most fundamental way to find information in a dataset is the Linear Search (also known as a sequential search). It works exactly how a human scans a list on paper: you start at the very first item and look at each one, one after the other, until you find what you are looking for or reach the end. While it is slower than a Binary Search for massive datasets, it is the only reliable way to search a list that hasn't been sorted.
In this guide, we will demonstrate how to perform a linear search using a FOR loop.
The Strategy: The Row-by-Row Scan
- Start at index 1.
- Compare the value at the current index with your
target. - If they match, record the index and stop.
- If they don't match, move to the next index.
Linear search works on any data, sorted or unsorted. It is the only search method that does not require the list to be pre-sorted, making it the safest default choice when you cannot guarantee the order of your data.
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: 1. Define the List
set "size=6"
set "ITEM_1=Alpha"
set "ITEM_2=Beta"
set "ITEM_3=Gamma"
set "ITEM_4=Delta"
set "ITEM_5=Epsilon"
set "ITEM_6=Zeta"
:: 2. Search Target
set "target=Delta"
set "foundIndex=-1"
echo Searching for "!target!" in the list...
:: 3. The Sequential Loop
for /L %%i in (1,1,%size%) do (
call set "val=%%ITEM_%%i%%"
echo Checking Index %%i: !val!
if /i "!val!"=="!target!" (
set "foundIndex=%%i"
:: Exit early to save time
goto :show_result
)
)
:show_result
echo.
if !foundIndex! GTR 0 (
echo [RESULT] Found "!target!" at Position !foundIndex!.
) else (
echo [RESULT] "!target!" was not found in the list.
)
endlocal
pause
Output:
Searching for "Delta" in the list...
Checking Index 1: Alpha
Checking Index 2: Beta
Checking Index 3: Gamma
Checking Index 4: Delta
[RESULT] Found "Delta" at Position 4.
Why Use Linear Search?
- Unsorted Data: If your list arrives in a random order (e.g., a "Current Tasks" list), you cannot use Binary Search. Linear Search is the only option.
- Small Lists: For lists with fewer than 50 items, the setup time for a complex search algorithm is often longer than just doing a quick linear scan.
- Broad Criteria: If you are searching for something that isn't a single "Primary Key" (e.g., "Find all users whose status is 'Logged In'"), a linear scan allows you to check every record easily.
Important Considerations
Linear search has O(n) time complexity. If you have 10,000 items and the target is the very last one (or doesn't exist), your script must perform all 10,000 comparisons. For large sorted datasets, use Binary Search instead.
- Efficiency: Linear search is O(n). If you have 10,000 items and the item you want is the very last one, your script will have to perform 10,000 comparisons.
- Early Exit: Always use
gototo exit the loop as soon as your item is found. There is no reason to continue scanning if you've already found your target. - Case Sensitivity: By default, Batch string comparisons in
ifare case-sensitive. Useif /ito ensure thatapplematchesApple.
Best Practices
- Multiple Matches: If you want to find every instance of a value, don't use
goto. Instead, append every matching index to a results array using a counter. - Findstr Alternative: If your data is in a text file rather than in memory variables, using the
findstrcommand is often faster than a manual loop.
To collect all matching indices, remove the goto exit and build a results array inside the match block: set /a "matchCount+=1" and set "MATCH_!matchCount!=%%i". After the loop, matchCount holds the total number of matches.
Conclusion
A linear search is the "Foundation Stone" of data processing logic. It provides a simple, reliable way to verify information and find coordinates within any unsorted dataset. By mastering the sequential scan and the "Early Exit" methodology, you can build Batch scripts that handle validation and lookup tasks with absolute accuracy, providing a solid logic base for more complex system automation.