【问题标题】:How to get collection item by index?如何按索引获取收藏项?
【发布时间】:2015-08-05 11:58:57
【问题描述】:

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

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

【问题讨论】:

  • 你尝试了什么,你得到了什么错误。
  • ---------------------------- Windows 脚本宿主 ------------ -------- ------- 场景:I:\123\scr.wsf Line:15 Character:2 Error: Invalid argument, or call the procedure code: 800A0005 Source: Runtime Error Microsoft VBScript - - - - - - - - - - - - - - 好的 - - - - - - - - - - - -----
  • 我需要 WScript.Echo 显示通过在文件夹“I:\123”中输入此文件的索引来选择的文件路径
  • 我也尝试过flash_folder_colFiles(1) 并得到同样的错误

标签: arrays indexing collections vbscript


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2021-03-08
    • 2017-03-17
    • 2015-06-13
    • 2015-10-02
    • 1970-01-01
    相关资源
    最近更新 更多