Test Existence of an element
We use the if defined functionality to check if an element is in the array or not. %1 is the first argument from the invoking command line. %~1 means the quotes surrounding the arguments are removed.
@ECHO OFF &SETLOCAL set "Array[Akash]=true" set "Array[John]=true" set "Array[Venky]=true" set "Array[Praveen]=true" set "MyUserName=Akash" call:check "%MyUserName%" set "MyUserName=Paul" call:check "%MyUserName%" goto:eof :check if defined Array[%~1] ( echo %~1 is in the array. ) else ( echo %~1 is NOT in the array. ) exit /b
The above code gives the below output
Akash is in the array. Paul is NOT in the array.
Batch Script – Arrays
An array is a collection of elements of the same data type. The arrays are not explicitly defined as Batch Script types but can be used. The following items need to be noted when the same members are used in Batch Script.
- Each aspect of the same members needs to be defined by a set order.
- A ‘for’ loop will be required to double the program values.
Contact Us