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:
The result will probably look like this:
USERDOMAIN=YOURPC USERNAME=You USERPROFILE=C:\Documents and Settings\You
Let's try another one:
The result will probably look somewhat like this:
PROCESSOR_ARCHITECTURE=x86 PROCESSOR_IDENTIFIER=x86 Family 6 Model 14 Stepping 8, GenuineIntel PROCESSOR_LEVEL=6 PROCESSOR_REVISION=0e08 ProgramFiles=C:\Program Files PROMPT=$P$G
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 ARCHITECTURE : x86 PROCESSOR IDENTIFIER : x86 Family 6 Model 14 Stepping 8, GenuineIntel PROCESSOR LEVEL : 6 PROCESSOR REVISION : 0e08
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 WMIC behaves a bit quirky, and its supposedly 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.
to list all values we just stored:
__LogicalDiskC.Caption=C: __LogicalDiskC.Compressed=FALSE __LogicalDiskC.CreationClassName=Win32_LogicalDisk __LogicalDiskC.Description=Local harddisk __LogicalDiskC.DeviceID=C: __LogicalDiskC.DriveType=3 __LogicalDiskC.FileSystem=NTFS __LogicalDiskC.FreeSpace=20930985984 __LogicalDiskC.MaximumComponentLength=255 __LogicalDiskC.MediaType=12 __LogicalDiskC.Name=C: __LogicalDiskC.QuotasDisabled=TRUE __LogicalDiskC.QuotasIncomplete=FALSE __LogicalDiskC.QuotasRebuilding=FALSE __LogicalDiskC.Size=31453437952 __LogicalDiskC.SupportsDiskQuotas=TRUE __LogicalDiskC.SupportsFileBasedCompression=TRUE __LogicalDiskC.SystemCreationClassName=Win32_ComputerSystem __LogicalDiskC.SystemName=YOURPC __LogicalDiskC.VolumeDirty=FALSE __LogicalDiskC.VolumeName=System __LogicalDiskC.VolumeSerialNumber=3647DF7B
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
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 December 2014, 10:51|