【问题标题】:hta vbs populate drop down menu with files in folderhta vbs 用文件夹中的文件填充下拉菜单
【发布时间】:2018-07-31 18:41:26
【问题描述】:

使用 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”的文件显示一个 msgbox

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

<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? 他们没有使用文件过滤器,并且鼠标悬停在填充上不是必需的,甚至不是想要的。

【问题讨论】:

标签: drop-down-menu vbscript onload hta auto-populate


【解决方案1】:

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

我已在临时文件夹中对此进行了测试以填充 *.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>

根据您的上一条评论

如何在下拉列表框中添加多个扩展文件?

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

【讨论】:

  • Hackoo 太棒了。只需按照您的建议更改全局变量以适应我的需要,Temp,Dir 和 Ext,它可以完美运行!要删除计数,只需删除“count = count + 1”并从:objOption.Text 行中删除计数?
  • 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 = fileNames objOption.Value = foundFile.path DropDown.Add(objOption) End If Next
  • 我只是注释掉了 count 行并修改了 objOption.Text 行。
  • Hackoo 有一部分代码我需要更改。输出值 = 完整目录名称 + file.ext 我如何将其更改为“file.ext”并从值中删除整个路径?
  • 你应该这样写 Foundfile.name
【解决方案2】:

这是一个例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多