VBS TUTORIAL: VBScript statement-for Each ... Next Statement _vbs

Source: Internet
Author: User

For Each ... Next statement

Repeats a set of statements for each element in an array or collection.

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



A variable used to enumerate all the elements in a collection or array. For a collection, theelement may be a variant variable, a universal object variable, or any specified Automation object variable. For an array, anelement can only be a variant variable.


The name of the collection or array of objects.


One or more statements executed for each item in the group .


If there is at least one element in the group , it goes into the for each block execution. Once you enter the loop, all the statements in the loop are executed first on the first element in the group . As long as there are other elements in the group , the statements in the loop are executed on each element. Exits the loop when there are no more elements in the group , and then continues execution from the statement following the next statement.

Exit for can only be used for each ... Next or for ... Next Control structure, provides another way to exit the loop. You can place any Exit for statements anywhere in the loop. Exit for often is used with conditional judgment statements (for example, If ...). Then), and immediately transfers control to the statement after next .

You can add a For each ... Next loops are placed in the other, forming a nested for each ... Next Loop. But the element of each loop must be unique.

Note If you omit an elementfrom the Next statement, the program continues to execute as if it already contains it. If the Next statement appears before its corresponding for statement, an error is generated.

The following examples illustrate how to use for each ... Next Statement:

Function ShowFolderList(folderspec)  Dim fso, f, f1, fc, s  Set fso = CreateObject("Scripting.FileSystemObject")  Set f = fso.GetFolder(folderspec)  Set fc = f.Files  For Each f1 in fc s = s & f1.name s = s & "<BR>" Next ShowFolderList = sEnd Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.