For Each ... Next Loop

Loop through the items in a collection or array.

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

   element     A variable used to hold each item in the group

   Group       A collection or a VBScript array


Set objFSO = CreateObject("Scripting.FileSystemObject")

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

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

“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-2023
Some rights reserved