arrays batch-file cmd delayedvariableexpansion windows

Arrays, linked lists and other data structures in cmd.exe (batch) script


I was playing with cmd.exe, but in its help I didn’t find any info, how to define arrays.

I have found, how to define simple variables:

set a=10
echo %a%

But, I want to create arrays, linked list etc…

So, does it able in cmd.exe ( I mean: does in cmd.exe exist any array keywords? )

I want to realize some algorithms as:

  • bubble sort
  • quick sort
  • gnome sort


So, I also want to know, does Cmd.exe have references or instances, structs etc?

Cause its help not full in: /?

Could Cmd.exe be defined as full by Turing-Machine definition? ( Turing-Complete )


  • 2

    @MatteoItalia linux shell has it, powershell ( based on .net ) has it, don’t know about Windows CScript.exe, does it have or not?

    – user1131997

    Apr 15, 2012 at 21:50


Windows shell scripting really isn’t designed to work with arrays, let alone complex data structures. For the most part, everything’s a string in the windows shell, but, there are some things you can do to “work with” arrays, like declaring n variables VAR_1, VAR_2, VAR_3... using a loop and filtering on the prefix VAR_, or creating a delimited string and then using the FOR construct that iterates over a delimited string.

Similarly, you can use the same basic idea to create a struct-like set of variables like ITEM_NAME, ITEM_DATA or w/e. I even found this link that talks about simulating an associative array in CMD.

It is all terribly hackish and inconvenient when it comes down to it. The command-line shell just wasn’t designed for heavy programming. I agree with @MatteoItalia — if you need serious scripting, use a real scripting language.


  • What do yo mean under serious? Could cmd.exe be defined as full by Turing Machine definition?

    – user1131997

    Apr 15, 2012 at 22:08

  • 2

    @magesi CMD does have ONE thing going for it — the FOR command. If you really want to learn CMD, master that and move on.

    Apr 15, 2012 at 22:19

  • @trutheality or there ie a way to write own cmd.exe based on NT4 sources, which are able 🙂 And include some new features to it 🙂

    – user1131997

    Apr 15, 2012 at 22:21

  • @trutheality thanks! 🙂 If I put here the result , may I call you to look at it? 🙂

    – user1131997

    Apr 15, 2012 at 22:26

  • 1

    @magesi: a more useful but crazy enough work would be to reverse-engineer every strange bit of batch syntax (I don’t think there’s an official specification even at Microsoft) and fix up the cmd.exe from the Wine project. 🙂

    Apr 15, 2012 at 22:31


I made a bubble sort implementation in batch using pseudo-arrays a while ago.
Not sure why you’d use it (although I will admit to doing so in another batch file) as it gets pretty slow as the list size increases. It was more to set myself a little challenge.
Someone might find this useful.

:: Bubblesort
:: Horribly inefficient for large lists
:: Dave Johnson implementation 05/04/2013
@echo off
setlocal enabledelayedexpansion
:: Number of entries to populate and sort
set maxvalue=50
:: Fill a list of vars with Random numbers and print them
for /l %%a in (1,1,%maxvalue%) do (
    set /a tosort%%a=!random!
:: echo them
set tosort
:: Commence bubble sort
Echo Sorting...
set /a maxvalue-=1
set iterations=0
for /l %%a in (%maxvalue%,-1,1) do ( REM Decrease by 1 the number of checks each time as the top value will always float to the end
    set hasswapped=0
        for /l %%b in (1,1,%%a) do (
            set /a next=%%b+1
            set next=tosort!next!
            set next=!next!
            call :grabvalues tosort%%b !next!
            rem echo comparing tosort%%b = !tosortvalue! and !next! = !nextvalue!
            if !nextvalue! LSS !tosortvalue! (
            rem set /a num_of_swaps+=1
            rem echo Swapping !num_of_swaps!
                set !next!=!tosortvalue!
                set tosort%%b=!nextvalue!
                set /a hasswapped+=1
    set /a iterations+=1
    if !hasswapped!==0 goto sorted
set tosortvalue=!%1!
set nextvalue=!%2!
::nice one our kid
set tosortvalue=
echo Iterations required: %iterations%
set tosort


  • 1

    Excuse me. I don’t like your “pseudo-arrays” reference. An array is mainly a concept: a set of elements with same name that are selected via an index. Your program may manage an array or may not; there is not such “pseudo-array” thing. See my previous link for further details…

    – Aacini

    Apr 13, 2014 at 3:08

  • 3

    @Aacini: There absolutely is such a thing. When you simulate arrays in a language that does not provide array constructs in its syntax or semantics, those may clearly and unambiguously be termed “pseudo-arrays”.

    Feb 20, 2016 at 17:15

  • @PreferenceBean: Excuse me. Do you know the set /A command? In this example: set /A resul=9+6, how would you call the 15 string stored in resul variable? “pseudo-number”? “simulated integer”? Remember that Batch files does not provide numeric variables!

    – Aacini

    Feb 20, 2016 at 18:04

  • @Aacini: Just so you know, the “Excuse me” you keep employing comes across as rude. I’m sure it was unintentional. But you do not need to be so defensive about MS Batch.

    Feb 20, 2016 at 18:19

  • 1

    @Aacini: I don’t know much about set /A, and I’m not claiming anything about Batch. I’m just saying, there certainly is such a concept as “pseudo-arrays” in the world. What Dave seems to be describing is like a set of PHP variables $var1 = 0; $var2 = 1; $var3 = 2; $i = 2; echo ${var$i};. Is that an array? No. Is it an attempt at simulating arrays? Yes. It’s a pseudo-array. This is what “pseudo” means.

    Feb 20, 2016 at 18:20