The ForEach loop is similar to the For loop, but it executes the statement block for each element in a collection, instead of a specified number of times.
ForEach Elementvariable In Collection
…
Next
(*) Elementvariable A variable name.
(r) Collection A COM/OLE collection object reference, or object property or method holding a collection.
For each iteration of the loop, WinBatch sets the variable Elementvariable to one of the elements in Collection and executes the statement block between the ForEach and Next. When all the elements in Collection have been assigned to Elementvariable, the ForEach loop terminates and control passes to the statement following the Next statement.
If Elementvariable is not used before the loop, it will be created for you. If it is used before the loop, the previously existing value of the variable is lost when the ForEach statement executes for the first time.
The elements of Collection can be of any data type, so the data assigned to elementvariable can be of any supported type including object references.
After the loop terminates Elementvariable contains the last element of the collection. If the elements of the collection are object references, you are responsible for releasing the last references with an assignment statement or the ObjectClose function.
To terminate a loop before the last element of the Collection is assigned to ElementVariable use a break statement. Goto statments are not permitted inside of ForEach...Next loops and will cause WinBatch to generate an error message.
It recommended that you not modify the Elementvariables in a ForEach...Next loop. Any modification you make may affect only the local copy of the element and may not be reflected back into the collection. Attempts to modify an element may also generate an error message
It is also recommended that you not alter the collection by adding, deleting, replacing, or reordering any elements. If you alter the collection after you have initiated a For Each...Next loop, the Collection object becomes invalid, and the next attempt to access an element may result in an error message or other unexpected behavior.
You can iterate through single dimension arrays (safearrays) returned by object properties and methods with a For Each...Next loop. However, you should not attempt to change array elements from within the loop.
Single dimension and multi-dimension regular WIL arrays, as well as, WIL maps can be iterated using the For Each...Next construct. When processing a WIL map, the ElementVariable contains a key of a key-value pair on each iteration. When processing regular WIL arrays, ElementVariable contains a copy of an array element on each iteration.
;This example uses a ForEach Next loop and WMI ;To collect information ON all installed printers ON the local machine. objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer") sPrinterInfo = "" ; Iterater through each printer and collect ForEach objPrinter In colInstalledPrinters sPrinterInfo = StrCat(sPrinterInfo, "Name: ", objPrinter.Name, @CRLF) sPrinterInfo = StrCat(sPrinterInfo, "Location: ", objPrinter.Location, @CRLF) If objPrinter.Default sYesNo = "Yes" Else sYesNo = "No" EndIf sPrinterInfo = StrCat(sPrinterInfo, "Default: ", sYesNo, @CRLF, @CRLF) Next Message("Installed Printers", sPrinterInfo) ; Clean up objPrinter = "" colInstalledPrinters = "" objWMIService = ""