O'Reilly logo

Microsoft® Windows® Scripting Self-Paced Learning Guide by Ed Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

For Each...Next

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required