onlyforbopi
1/13/2017 - 12:25 PM

DOS_TUTORIAL

DOS_TUTORIAL

::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Create an array (1st method)
set a[0] = 1
set a[1] = 3

:: Create an array (2nd method)
::Another way to implement arrays is to define a list of values and iterate 
::through the list of values. 
::The following example show how this can be implemented.

@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]%

:: MODIFYING ALREADY EXISTING VALUE
@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
:: KNOWN SIZE
@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]! 
)

:: UNKNOWN SIZE
set sources[0]=0
set sources[1]=0
set sources[2]=0for /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
@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 ARRAY STRUCTURES
@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