Search code examples
batch-filedos

Win32 Batch Script, Search a String Data and Determine the Group Name


I have a data text file in which data is divided into groups as follow:

[Group 1]

string-1

string-2

string-3


[Group 2]

string-4

string-5

string-6

string-7

string-8

[Group 3]

string-9

string-10

When the batch script runs, it will ask for an input which will be one of the strings; it then will search for the input string in the file and report the group the string is found. (The strings have no spaces.)


Solution

  • Like this :

    @echo off
    
    set /p "$UserInput=Enter the string : "
    
    setlocal enabledelayedexpansion
    set /a $c=0
    for /f "delims=" %%a in (data.txt) do (
     set $line=%%a
     if "!$line:~0,1!"=="[" set /a $c+=1
     if /i !$line!==%$UserInput% goto:next
    )
    exit /b
    
    :next
    echo User is group is : [Group!$c!]