Using Arrays in Batch Files

One thing the batch language does not use is strongly typed variables.
In batch files, everything is a string, and a string can be anything ranging from numbers to commands. This may be a weak point of the batch language, it can also be used to your advantage by creating "Q&D" (Quick & Dirty) solutions that would never be possible in other languages.

Having worked with several other scripting languages, I really did miss (associative) arrays in the batch language.
On this page I'll show you how, in Windows NT 4 and later, to use sets of variables like they were (associative) arrays or hashtables.

Type the following command on the DOS-prompt:

SET User

The result will probably look like this:

USERPROFILE=C:\Documents and Settings\You

Let's try another one:


The result will probably look somewhat like this:

PROCESSOR_IDENTIFIER=x86 Family 6 Model 14 Stepping 8, GenuineIntel
ProgramFiles=C:\Program Files

Now let's see if we can loop through all PROCESSOR_* variables, like they were elements in an array (in batch files, use %%A instead of %A, and ECHO instead of @ECHO):

FOR /F "tokens=2* delims=_=" %A IN ('SET PROCESSOR_') DO @ECHO PROCESSOR %A  :  %B

This is what you'll get:

PROCESSOR IDENTIFIER  :  x86 Family 6 Model 14 Stepping 8, GenuineIntel

That is the complete list of all PROCESSOR_* variables!

To create your own arrays, I suggest using dots instead of underscores, and maybe prefix the name with a double underscore.
The following command uses WMIC and a "Q&D" batch shortcut to store all properties of harddisk partition C: in an array-like set of environment variables, and uses FOR /F to loop through this array-like set of variables:

FOR /F "tokens=*" %%A IN ('WMIC LogicalDisk Where "DeviceID='C:'" Get /Format:list ^| FIND "="') DO (
	SET __LogicalDiskC.%%A

or if we only want to store variables with non-empty values:

FOR /F "tokens=*" %%A IN ('WMIC LogicalDisk Where "DeviceID='C:'" Get /Format:list ^| FINDSTR /R /C:"=."') DO (
	SET __LogicalDiskC.%%A

You may be surprised to find that these commands have different results.
After all, one might ask, aren't environment variables that get an empty value discarded?
That's right, but the so-called empty values still contain CR/LF pairs (Carriage Return/Line Feed, or "Enter"), so they will show up, even if they seem to have no value assigned.

Now, type:

SET __LogicalDiskC.

to list all values we just stored:

__LogicalDiskC.Description=Local harddisk

To loop through all values, use the same, slightly modified FOR /F loop again:

FOR /F "tokens=2* delims=.=" %%A IN ('SET __LogicalDiskC.') DO ECHO Drive C: %%A = %%B

And behold:

Drive C: Caption = C:
Drive C: Compressed = FALSE
Drive C: CreationClassName = Win32_LogicalDisk
Drive C: Description = Local harddisk
Drive C: DeviceID = C:
Drive C: DriveType = 3
Drive C: FileSystem = NTFS
Drive C: FreeSpace = 20931293184
Drive C: MaximumComponentLength = 255
Drive C: MediaType = 12
Drive C: Name = C:
Drive C: QuotasDisabled = TRUE
Drive C: QuotasIncomplete = FALSE
Drive C: QuotasRebuilding = FALSE
Drive C: Size = 31453437952
Drive C: SupportsDiskQuotas = TRUE
Drive C: SupportsFileBasedCompression = TRUE
Drive C: SystemCreationClassName = Win32_ComputerSystem
Drive C: SystemName = YOURPC
Drive C: VolumeDirty = FALSE
Drive C: VolumeName = System
Drive C: VolumeSerialNumber = 3647DF7B

Instead of just ECHOing, you can use the variables any other way you want.

This may be the closest approximation to (associative) arrays you'll ever get in batch language.



page last uploaded: 4 March 2011, 12:48