The For Each...Next tool is really the "For something—do it again" tool. For Each...Next lets you walk through a collection of objects and perform a particular action on the object, and then perform that action again on the next object.
In the following script, you use For Each...Next to examine characteristics of fixed drives on a server:
Option Explicit On Error Resume Next Const DriveType = 3 Dim colDrives Dim drive set colDrives = _ GetObject("winmgmts:").ExecQuery _ ("select DeviceID from Win32_LogicalDisk where DriveType =" & DriveType) For Each drive in colDrives WScript.Echo drive.DeviceID Next
Let’s peruse this script and see what it’s doing. In your initial reading, you see some common elements you learned about in Chapter 1 ...