Search code examples
arraysbatch-filecmd

Batch script how to compare array of strings


I have array like a[0]= ABC , a[1]=ABC , a[2]=ABC and I want to compare it using :

if  %a[0]%==%a[1]%=%a[2]% ( echo Equal)

Comparing three is not working but comparing two works fine.

if  %a[0]%==%a[1]% ( echo Equal)

any suggestion, please ?


Solution

  • To check multiple (pseudo-)array elements for equality, you could try this:

    setlocal EnableDelayedExpansion
    set "FLAG=#"   & rem // (flag that is going to be cleared in case of an encountered inequality)
    set /A "MAX=9" & rem // (greatest array index number to be included in the comparison loop)
    for /L %%I in (1,1,%MAX%) do if not "!a[0]!"=="!a[%%I]!" set "FLAG="
    if defined FLAG echo All array elements from index 0 to %MAX% are equal.
    endlocal