如何通过索引获取集合项?

时间:2015-08-05 11:58:58

标签: arrays indexing collections vbscript

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

<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>

1 个答案:

答案 0 :(得分:1)

索引无法访问.Files集合:

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

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

>> 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