VBScript Basics, Part 30 | For – Each – Next – Loops
VBScripting (.vbs) Basic tutorial on how to use a For Next Loop, and For Each Next Loop, to loop things a certain amount of times or to loop through a set of objects in a group, such as files and folders inside of a folder.
Code Example:
______________________________________________________
Option Explicit
Dim objFSO, objFolder, x
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(“C:UsersJeremyDownloads”)
For Each x In objFolder.SubFolders
If x.Name = “Dad” then
Exit For
Else
MsgBox x.Name
End If
Next
______________________________________________________
Option Explicit
Dim Login, tries
For tries = 1 To 3
Login = InputBox(“Type in the password”)
If Login = “password” Then
Exit For
ElseIf tries = 3 Then
MsgBox “Sorry, you ran out of tries.”, vbCritical
Wscript.Quit
End If
Next
______________________________________________________
Know the Basics:
——————————————————————————————
For countVariable = 1 To Max#
‘code here
Next
For Each object In objects
‘code here
Next
——————————————————————————————