hta vbs 用文件夹中的文件填充下拉菜单

hta vbs populate drop down menu with files in folder

使用 Onload 命令,我可以从消息框的文件夹中输出相关文件,但无法理解如何使用该信息来填充 html 代码中的下拉菜单。

Sub Window_onLoad
    LoadDropDown
End Sub

Sub LoadDropDown
    Dim dir, foundFile
    dir = zipfolder
    Dim fileNames, fso, folder
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(dir)
    For Each foundFile In folder.Files
        fileNames = foundFile.name
        If(Right(fileNames,4) = ".zip") then
        fileNames = Left(fileNames,(Len(fileNames)-4))
        Value = Value & fileNames & vbCr 
        MsgBox "inside sub Value : " & Value
    End If 
    Next
End Sub

这将为找到的每个扩展名为“.zip”的文件显示一个消息框

令人困惑的部分是如何在下拉菜单中显示此信息(加载时)??? 我从下面遗漏了什么?

<select id="test" name="test" onchange="LoadDropDown" style="width: 336px;">
        <option value=""></option>
        </select>

提前感谢您的帮助!

这与: How to output all sub-folder to a drop down list in a HTA? 他们没有使用文件过滤器,并且鼠标悬停在填充上不是必需的,甚至不是想要的。

这是一个例子:

<html>
<head>

<script language="vbscript">

Sub Init
    document.getElementById("option1").innerText = "Sample 1"
    document.getElementById("option2").innerText = "Sample 2"
End Sub

</script>

</head>
<body onLoad="Init()">

<select id="test" name="test" style="width: 336px;">
    <option id="option1"></option>
    <option id="option2"></option>
</select>

</body>
</html>

您可以像这样尝试自动填充您的下拉菜单:

我已经在临时文件夹中测试过这个来填充 *.tmp 个文件,因此您可以根据需要进行更改

<html>
<HTA:APPLICATION ICON="magnify.exe"/>
<head>
<Title>Load DropDown Menu</Title>
<script language="vbscript">
Option Explicit
Dim ws,Temp,dir,objOption,Ext
Set ws = CreateObject("WScript.Shell")
Temp = ws.ExpandEnvironmentStrings("%Temp%")
Dir = Temp
Ext = "tmp"
'---------------------------------------------------------------
Sub Window_onLoad
    Call LoadDropDown(Dir,Ext)
End Sub
'---------------------------------------------------------------
Sub LoadDropDown(Dir,Ext)
Dim fso,folder,foundFile,fileNames,objOption,Count
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(Dir)
Count = 0
Call ClearListbox()
For Each foundFile In folder.Files
    fileNames = FSO.GetBaseName(foundFile)
    if Lcase(fso.getExtensionName(foundFile.path)) = Lcase(Ext) then
        Count = Count + 1
        Set objOption = Document.createElement("OPTION")
        objOption.Text =  Count & " - " & fileNames 
        objOption.Value = foundFile.path
        DropDown.Add(objOption) 
    End If 
Next  
End Sub
'---------------------------------------------------------------
Sub ClearListbox()
    For Each objOption in DropDown.Options
        objOption.RemoveNode
    Next 
End Sub
'---------------------------------------------------------------
Sub Explorer(File)
    MsgBox File
    ws.run "Explorer /n,/select,"& File &"",1,True
End Sub
'---------------------------------------------------------------
</script>
</head>
<select id="DropDown" name="DropDown" onchange="Explorer(DropDown.value)" style="width: 336px;">
</select>
</body>
</html>

根据您最后的评论

How can i add more than extension file in the dropdown listbox ?

<html>
<HTA:APPLICATION ICON="magnify.exe"/>
<head>
<Title>Load DropDown Menu</Title>
<script language="vbscript">
Option Explicit
Dim ws,Temp,dir,objOption,ArrayExtensions,Ext
Set ws = CreateObject("WScript.Shell")
Temp = ws.ExpandEnvironmentStrings("%Temp%")
Dir = Temp
ArrayExtensions = Array("exe","bat","cmd","vbs","ps1","zip","rar","tmp")
'---------------------------------------------------------------
Sub Window_onLoad
    Call ClearListbox()
    For each Ext in ArrayExtensions
        Call LoadDropDown(Dir,Ext)
    Next
End Sub
'---------------------------------------------------------------
Sub LoadDropDown(Dir,Ext)
Dim fso,folder,foundFile,fileNames,objOption,Count
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(Dir)
Count = 0
For Each foundFile In folder.Files
    fileNames = FSO.GetBaseName(foundFile)
    if Lcase(fso.getExtensionName(foundFile.path)) = Lcase(Ext) then
        Count = Count + 1
        Set objOption = Document.createElement("OPTION")
        objOption.Text =  "[" & Ext & "] - " & Count & " - " & foundFile.Name 
        objOption.Value = foundFile.path
        DropDown.Add(objOption) 
    End If 
Next  
End Sub
'---------------------------------------------------------------
Sub ClearListbox()
    For Each objOption in DropDown.Options
        objOption.RemoveNode
    Next 
End Sub
'---------------------------------------------------------------
Sub Explorer(File)
    MsgBox File
    ws.run "Explorer /n,/select,"& File &"",1,True
End Sub
'---------------------------------------------------------------
</script>
</head>
<select id="DropDown" name="DropDown" onchange="Explorer(DropDown.value)" style="width: 336px;">
</select>
</body>
</html>