如何通过索引获取集合项? [英] How to get collection item by index?

查看:18
本文介绍了如何通过索引获取集合项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问集合中的项目?下一个代码在最后一行给了我错误.

How to access item in collection? Next code gives me error in last line.

<package>
 <job id="NonDisabledServicesCollecting">
<COMMENT>
************************************************************
1 comment
2 
3 
************************************************************
</COMMENT>
  <script language="VBScript">
flash_folder="I:123"
str_flash_folder_colFiles = ""
num_flash_folder_colFiles = 0
Set flash_folder_colFiles = CreateObject("Scripting.FileSystemObject").GetFolder(flash_folder).Files
WScript.Echo  flash_folder_colFiles(1)
For Each flash_folder_objFile in flash_folder_colFiles
  num_flash_folder_colFiles = num_flash_folder_colFiles + 1
  str_flash_folder_colFiles = str_flash_folder_colFiles + cstr(num_flash_folder_colFiles) + " " + flash_folder_objFile.Name + vbCrLf
Next

Dim response
Do
  response = InputBox("Please enter the number that corresponds to your selection:" + vbCrLf + str_flash_folder_colFiles, "Choose DLL to copy...")
  If response = "" Then WScript.Echo "Input is empty." 'Detect Cancel
  If IsNumeric(response) Then Exit Do                  'Detect value response.
  WScript.Echo "You must enter a numeric value."
Loop
selected_flush_DLL = flash_folder + flash_folder_colFiles(cint(response))
WScript.Echo selected_flush_DLL

  </script>
 </job>
</package>

推荐答案

无法通过索引访问 .Files 集合:

The .Files collection can't be accessed by index:

>> Set oFiles = goFS.GetFolder(".").Files
>> n = oFiles(0).Name
>>
Error Number:       5
Error Description:  Invalid procedure call or argument

您需要一个 For Each 循环来填充随机访问集合,例如一个数组:

You need a For Each loop to fill a random access collection e.g. an Array:

>> Set oFiles = goFS.GetFolder(".").Files
>> ReDim aFiles(oFiles.Count - 1)
>> i = 0
>> For Each oFile In oFiles
>>     Set aFiles(i) = oFile
>> Next
>> n = aFiles(0).Name
>> WScript.Echo n
>>
31823568.notes

这篇关于如何通过索引获取集合项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆