DOS_TUTORIALS
: Creating an array
set a[0] = 1
:: Optional way to create array - Iterate through list of values
@echo off
set list = 1 2 3 4
(for %%a in (%list%) do (
echo %%a
))
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ACCESSING ARRAY ELEMENTS
@echo off
set a[0] = 1
set a[1] = 2
set a[2] = 3
echo The first element of the array is %a[0]%
echo The second element of the array is %a[1]%
echo The third element of the array is %a[2]%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: MODIFYING ARRAY
@echo off
set a[0] = 1
set a[1] = 2
set a[2] = 3
Rem Adding an element at the end of an array
Set a[3] = 4
echo The last element of the array is %a[3]%
::You can modify an existing element of an Array by
::assigning a new value at a given index as shown in the following example −
@echo off
set a[0] = 1
set a[1] = 2
set a[2] = 3
Rem Setting the new value for the second element of the array
Set a[1] = 5
echo The new value of the second element of the array is %a[1]%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ITERATING OVER ARRAY
:: CASE 1 - WE KNOW SIZE
Iterating Over an Array
Iterating over an array is achieved by using the ‘for’ loop and going through each element of the array. The following example shows a simple way that an array can be implemented.
@echo off
setlocal enabledelayedexpansion
set topic[0] = comments
set topic[1] = variables
set topic[2] = Arrays
set topic[3] = Decision making
set topic[4] = Time and date
set topic[5] = Operators
for /l %%n in (0,1,5) do (
echo !topic[%%n]!
)
:: CASE 2 - WE DONT
set sources[0]=C:\Users\P.Doulgeridis\Desktop\SAPRUN00FILE\AAyymmddm1.txt
set sources[1]=C:\Users\P.Doulgeridis\Desktop\SAPRUN00FILE\AAyymmddm1EPAN.txt
set sources[2]=C:\Users\P.Doulgeridis\Desktop\SAPRUN00FILE\AAyymmddm1PPC.txt
set sources[3]=C:\Users\P.Doulgeridis\Desktop\SAPRUN00FILE\ALyymmddm1.V51.txt
set sources[4]=C:\Users\P.Doulgeridis\Desktop\SAPRUN00FILE\ALyymmddm1.V52.txt
for /F "tokens=2 delims==" %%s in ('set sources[') do (
IF NOT EXIST %%s (
CALL:error10 "225" "UNABLE TO LOCATE TARGET FILE %%s "
CALL:ERRORNOTIFYMAIL "UNABLE TO LOCATE TARGET FILE %%s" "FATAL" "CHECK EXISTENCE OF TARGET FILES"
EXIT /B 11
) ELSE (
ECHO TARGET DIRECTORY %%s LOCATED SUCCESFULLY
ECHO.
)
set /a "numberoffilesout+=1"
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: LENGTH OF ARRAY
::Length of an Array
::The length of an array is done by iterating over the list
::of values in the array since there is no direct function
::to determine the number of elements in an array.
@echo off
set Arr[0] = 1
set Arr[1] = 2
set Arr[2] = 3
set Arr[3] = 4
set "x = 0"
:SymLoop
if defined Arr[%x%] (
call echo %%Arr[%x%]%%
set /a "x+=1"
GOTO :SymLoop
)
echo "The length of the array is" %x%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: CREATING STRUCTURES IN ARRAYS
Creating Structures in Arrays
Structures can also be implemented in batch files using a little bit of an extra coding for implementation. The following example shows how this can be achieved.
Example
@echo off
set len = 3
set obj[0].Name = Joe
set obj[0].ID = 1
set obj[1].Name = Mark
set obj[1].ID = 2
set obj[2].Name = Mohan
set obj[2].ID = 3
set i = 0
:loop
if %i% equ %len% goto :eof
set cur.Name =
set cur.ID =
for /f "usebackq delims ==. tokens = 1-3" %%j in (`set obj[%i%]`) do (
set cur.%%k = %%l
)
echo Name = %cur.Name%
echo Value = %cur.ID%
set /a i = %i%+1
goto loop
::The following key things need to be noted about the above code.
::Each variable defined using the set command has 2 values associated
::with each index of the array.
::The variable i is set to 0 so that we can loop through
::the structure will the length of the array which is 3.
::We always check for the condition on whether the value
::of i is equal to the value of len and if not, we loop through the code.
::We are able to access each element of the structure using the obj[%i%] notation.