如何在 VBScript 中将项目添加到文件夹项目 3?

How to add item to folder item 3 in VBScript?

我想将一个项目添加到项目列表,但我不知道该怎么做。

Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = createobject("Scripting.FileSystemObject")
strFontSourcePath = "C:\Fonts"
strFontSourcePath1 = "C:\Fonts2"
Set objNameSpace = objShell.Namespace(strFontSourcePath)
Set objNameSpace1 = objShell.Namespace(strFontSourcePath1)
Set objFolder = objNameSpace.Items()
Set list = objNameSpace1.Items()

For Each objFile In objFolder
    If LCase(Right(objFile, 4)) = ".ttf" Or LCase(Right(objFile, 4)) = ".otf" Then
       'add objFile to list?
    End If
Next

我尝试了不同的方法,但我做不到。例如我试试这个

list.Add objFile 

或者这个

ReDim Preserve list(UBound(list) + 1)
list(UBound(list)) = objFile

for more information : type of list and objFolder is folder item 3

有人可以帮忙吗?

错误消息试图告诉您 list 是类型 FolderItems3 的集合。集合代表文件夹中的项目,因此您不能只向其中添加内容。如果您想构建两个或多个文件夹的项目组合列表,您需要将 list 设为常规数组并将所有文件夹中的项目添加到其中:

fontSourcePaths = Array("C:\Fonts", "C:\Fonts2")
ReDim list(-1)  'inintialize empty array

For Each dir In fontSourcePaths
    Set ns = objShell.Namespace(dir)
    For Each f In ns.Items
        If objFSO.FileExists(f.Path) Then
            ReDim Preserve list(UBound(list)+1)
            Set list(UBound(list)) = f
        End If
    Next
Next

根据需要调整向数组添加项的条件。

请注意,在循环中追加到数组是一项开销很大的操作,仅应在项数较少时使用。对于更大的数字,使用字典之类的东西可以获得更好的性能:

Set dict = CreateObject("Scripting.Dictionary")

For Each dir In fontSourcePaths
    Set ns = objShell.Namespace(dir)
    For Each f In ns.Items
        dict(f) = True
    Next
Next

list = dict.Keys

或 ArrayList:

Set list = CreateObject("System.Collections.ArrayList")

For Each dir In fontSourcePaths
    Set ns = objShell.Namespace(dir)
    For Each f In ns.Items
        list.Add f
    Next
Next