Enum Reference

Reference for the Enum object, which enumerates COM collections (such as FolderItems, the Drives of Scripting.FileSystemObject, and TabCtrl) one element at a time. It corresponds to JScript's Enumerator.

Notation: M = method. Member names are matched case-insensitively.

Table of Contents

  1. How to Obtain
  2. Methods
  3. What Can Be Enumerated
  4. Notes
  5. Examples

1. How to Obtain

api.CreateObject("Enum", collection)

const e = api.CreateObject("Enum", fso.Drives);

2. Methods

Name Return value Description
atEnd() bool true if the enumeration has finished (or the object cannot be enumerated)
item() value Returns the current element. Do not call it while atEnd() is true (the value is unspecified)
moveNext() none Advances to the next element
moveFirst() none Returns to the beginning and fetches the first element

A typical loop looks like this (the same as with Enumerator).

for (const e = api.CreateObject("Enum", collection); !e.atEnd(); e.moveNext()) {
    const item = e.item();
}

3. What Can Be Enumerated

The object you pass is checked in the following order, and the first condition that matches is used.

Object passed What is enumerated
IShellWindows (Windows() of Shell.Application) The open shell windows
FolderItems (FV.Items(), FV.SelectedItems(), te.FolderItems(), etc.) FolderItem
An object that has _NewEnum (DISPID_NEWENUM) The elements returned by its enumerator (many COM collections other than fso.Drives, TabCtrl, Memory, and WICBitmap)
Any other IDispatchEx Returns the member names (property names) one by one as strings (like for…in)
Anything else Nothing is enumerated; atEnd() is true from the start

4. Notes


5. Examples

// Enumerate drives (Scripting.FileSystemObject)
const fso = api.CreateObject("Scripting.FileSystemObject");
for (const e = api.CreateObject("Enum", fso.Drives); !e.atEnd(); e.moveNext()) {
    const drive = e.item();
    // drive.DriveLetter, drive.TotalSize ...
}

// Enumerate FolderItems
const Items = FV.SelectedItems();
for (const e = api.CreateObject("Enum", Items); !e.atEnd(); e.moveNext()) {
    const path = e.item().Path;
}

// Enumerate property names
const o = api.CreateObject("Object");
o.a = 1;
o.b = 2;
for (const e = api.CreateObject("Enum", o); !e.atEnd(); e.moveNext()) {
    const key = e.item();     // "a", "b"
}

// Asynchronous script (WebView2 side)
for (let list = await api.CreateObject("Enum", collection); !await list.atEnd(); await list.moveNext()) {
    const item = await list.item();
}

To enumerate a FolderItems, for...in or index access is usually simpler (FolderItems can be enumerated with Count and Item(i), and TabCtrl can also be enumerated with for...in). Use Enum for COM collections that provide only _NewEnum, and for enumerating member names.