For Each ... Next Loop

Loop through the items in a collection or array.

Syntax 
      For Each element In Group
        [Statements]
        [Exit For]
        [Statements]
      Next [element]

Key
   element     A variable used to hold each item in the group

   Group       A collection or a VBScript array

Examples

Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objDrive In objFSO.Drives
  WScript.Echo objDrive.DriveType
Next

Set objFolder = objFSO.GetFolder("C:\Work")
Set arrFolders = objFolder.SubFolders
For Each strFolderName In arrFolders
  WScript.Echo strFolderName
Next

“Profit in business comes from repeat customers, customers that boast about your project or service, and that bring friends with them” ~ W. Edwards Deming

Related VBScript commands

For...Next - Repeat a block of statements a given number of times.
Do..Loop - Repeat a block of statements.
Equivalent PowerShell cmdlet: ForEach-Object


 
Copyright © 1999-2024 SS64.com
Some rights reserved