Search code examples
drop-down-menuvbscriptonloadhtaauto-populate

hta vbs populate drop down menu with files in folder


Using an Onload command I can output the relevant files from a folder in a messagebox but cannot understand how to use that information to populate a drop down menu in the html code.

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

This will display a msgbox for each file found with extension ".zip"

The confusing part is how to display this information (on load) in a drop down menu??? What am I missing from the below?

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

Thank you in advance for any help!

This is NOT the same as: How to output all sub-folder to a drop down list in a HTA? They are not using a file filter and mouseover on populate is NOT what is required or even wanted.


Solution

  • You can try like this to auto-populate your drop down menu :

    I have tested this in the temporary folder to populate *.tmp files, so you can change it for your needs

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

    Based on your last comment

    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>