Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Batch Script - Length of an Array
Next article icon

Batch Script - Arrays

Last Updated : 08 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

How to create an array?

We can create an array using the command 'set'. For example, set a[0] = 1 means we created an array whose first element of index 0 has a value of 1. Also, another way to define an array is by creating a list and then looping through it. Let us look at the below example.

@echo off set list_1=7 9 1 3 (for %%p in (%list_1%) do (   echo %%p )) 

The above command produces the below output

7 9 1 3
Fig 1.1 Create Array and Add Elements

How to Access an Array and its elements?

The easiest way to access an array is by using its Index. In the above example, the first element can be accessed as a[0], second with a[1], and so on. The index starts from 0 and ends with length(array)-1. Let's look at the below example, where we have 2 elements in the array and we are accessing the values by their index.

@echo off  set a[0]=5  set a[1]=12  echo The first element of the array created is %a[0]%  echo The second element of the array created is %a[1]% 

The Above code will produce the following output.

The first element of the array created is 5  The second element of the array created is 12  
Fig 1.2 Accessing Elements Inside Array

How to modify the elements of an array?

We can add a new element or modify the value of an existing element by using its index. The output of the below program is 'last element is 12'

@echo off  set a[0]=5  set a[1]=7    set a[2]=9  Rem Adding a new element at the array end  Set a[3]=12  echo The last element is %a[3]% 

Now let us look at modifying the existing value. In the below snippet, we assign a[2] as 14 and the third element of the array is now 14

Set a[2]=14

echo The new value of the third element of the array is %a[2]% 

The output of the above snippet results in the below behavior

The new value of the third element of the array is 14 

Fig 1.3 Modify Elements in an Array

Iterating over an Array

Iterating over an array is achieved using for loop. Using for loop, we can access elements one by one. Let us look at the example below. /l is used here, as we move through the specified range and iterate through the Array.

@echo off  set a[0]=5  set a[1]=7    set a[2]=9  Rem Adding a new element at the array end  Set a[3]=12  echo The last element is %a[3]% 

The Above snippets produce the below Output

 Download  Upload   Browse   Add   Save   Delete 
Fig 1.4 Iterating Over an Array

Length of an Array

There is no predefined function to find the length of the array. We can use for loop and Iterate through the Array.

@echo off  :: Here an array is defined set array[0]=1  set array[1]=4  set array[2]=9  set array[3]=10    :: Here we initializing an variable named  :: len to calculate length of array set len=0   :: To iterate the element of array :Loop   :: It will check if the element is defined or not if defined array[%len%] (  set /a len+=1 GOTO :Loop  ) echo The length of the array is %len% pause

The above program produces the below output.

The length of the array is 4
Fig 1.5 Length of anArray

Creating Structures inside Arrays

We can also create structures inside an array. In the below example, Each of the variables defined using the default command (set) has 2 values ​​associated with each of the identical members. The variable i is set to 0 so that we can move through the structure and the loop will run 3 times. We constantly check the status of whether i value is equal to len value and if not, we enter with code. We can access each component of the structure using obj [%i%] notation.

@echo off  set obj[0].Name=Akash  set obj[0].ID=101  set obj[1].Name=Ajay  set obj[1].ID=102  set obj[2].Name=Thomas  set obj[2].ID=103     FOR /L %%i IN (0 1 2) DO  (      call echo User Name = %%obj[%%i].Name%%      call echo User ID = %%obj[%%i].ID%%  ) 

The above code produces the below output

User Name = Akash  User ID = 101  User Name = Ajay  User ID = 102  User Name =  Thomas User ID = 103
Fig 1.6 Creating Structures inside Arrays

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.
Fig 1.7 Test element in an array

Next Article
Batch Script - Length of an Array

A

Akash7
Improve
Article Tags :
  • Linux-Unix
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Batch-script

Similar Reads

  • Bash Scripting - Array
    Arrays are important concepts in programming or scripting. Arrays allow us to store and retrieve elements in a list form which can be used for certain tasks. In bash, we also have arrays that help us in creating scripts in the command line for storing data in a list format. In this article, we will
    7 min read
  • Batch Script - Aliases
    Be it Linux, macOS, or Windows, we sometimes need to use the terminal or the command line to perform certain commands. If in such situations, we find ourselves repeating a few long commands we can use an alias to save time and make it easier. In windows, we can create an alias as a batch command fro
    6 min read
  • Batch Script - Length of an Array
    The length of the array is the number of elements in the array. The index of the array starts from "0" to "N-1" where N is a number of elements. For example arr[0]=1 arr[1]=2 arr[2]=3 Here we can see that the index starts from 0 and ends with 2 . So, we know that the index of the element goes from 0
    2 min read
  • Basics of Batch Scripting
    Batch Scripting consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. It is not commonly used as a programming language and so it is not commonly practiced and is not trending but its control and dominance in the Windows environment can never b
    4 min read
  • Batch Script - Iterating Over an Array
    A batch script is a set of instructions or a script for SOS and Windows Operating System. We can write them in the CMD line by line or we can create a file with a ".bat" or ".cmd" extension. The file can contain valid instructions for executing by the interpreter. The meaning of batch is the non-int
    3 min read
  • Batch Script - Align Right
    We can use Batch scripting for manipulating the data. We have certain commands and filters to manipulate and edit certain pieces of data very easily for better visualization. Align Right in Batch scripting is one of the commands/filters that helps in aligning or arranging text in a desired manner. U
    3 min read
  • Batch Script - toInt
    In the Batch script, every variable is considered as a string and optionally integers. We need numbers all the time especially when it comes to performing system operations. We might extract numbers from a string variable but performing operations on them are not the same and require some additional
    3 min read
  • Batch Script - How to Modifying an Array
    In this article, we are going to learn how we can modify any array using Batch Script. We can modify any array in two ways. We can add elements in any array or we can replace elements of any array. Modify an array by adding an element. Code : @echo off set arr[0]=Geeks set arr[1]=for set arr[2]=Geek
    2 min read
  • C# | Arrays of Strings
    An array is a collection of the same type variable. Whereas a string is a sequence of Unicode characters or array of characters. Therefore arrays of strings is an array of arrays of characters. Here, string array and arrays of strings both are same term. For Example, if you want to store the name of
    4 min read
  • Batch Script - Creating Structures in Arrays
    In this article, we'll understand how to create structures in arrays in a batch script. What are Structures? Structures are like a multi-type list of values, unlike arrays we have a list of single type values. Let's say we have an array called person, we have initially declared to a list of names, b
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences