vbscript - HTA and VBS dynamic list and opening file in list -
Firstly new and generally programming here. I am trying to create an HTA that can load different VBS scripts from an external folder so that it can be made more modular. I am trying to open VBS from my dynamic list. How do I open a file in my dynamic list? And how can I pass a variable in the file as well? I currently have this:
& lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; My HTML Application & lt; / Title & gt; & Lt; HTA: Application APPLICATIONNAME = "My HTML Application" ID = "MyAlation" VERSION = "1.0" /> & Lt; / Head & gt; & Lt; Script language = "VBScript" & gt; Deputy Window_OnLoad for dim FolderPath 'folder dim objFSO dim objFolder dim colFiles dim objFile dim objOption FolderPath = "% PathToScripts%" set objFSO = CreateObject ( "Scripting.FileSystemObject") objFolder = objFSO.GetFolder (FolderPath) set colFiles set go to = objFolder.Files colFiles set objOption = document.createElement ( "options") objOption.Text = objFile.Name objOption.Value = objFile.Name mylistbox.Add (objOption) Next End sub sub RunProgram set objShell = CreateObject For each objFile ("Wscript.Shell") objShell.Run objOption End sub & lt; / Script & gt; & Lt; Body bgcolor = "white" & gt; & Lt ;! - Add your controls here - & gt; & Lt; Select name = "mylistbox" size = 10> & Lt; / Select & gt; & Lt; Input type = "button" value = "single selection" onclick = "runprogram" name = "rescript" & gt; & Lt ;! - {{InsertControlsHere}} - Do not remove this line - & gt; & Lt; / Body & gt; & Lt; / Html & gt;
Question 1: How do I open the file dynamic list?
First of all, you need to recover the selected value from your list. For single selection lists, you can just
query the value
of the property's & lt; Select & gt;
element:strFile = mylistbox.Value
Since nothing can be selected, it It is always a good idea to test the result for sure:
If lane (strfile)> 0 then
Also, it seems that you are showing the file name in the list, not the file path , which is fine , But if you have to run the file later, you will need the full file path, so you have a few options, here.
Option 1:
Make a global stability instead of a local variableFolderpath
so that you can access it from yourRnprogram ()
routine. Take advantage ofOption 2:
Value
; Options & gt;
element still stores the full path for each list item, still displaying the name of the file. Here's how to do the latter:set objOption = document.createElement ( "Options") went replaceable objFile.Name the objOption.Text = objFile.Name objOption.Value = objFile.Path 'objFile .path mylistbox.Add (objoption) Next
Now that you have the complete path to your script, you can run it. This your
run program ()
might look like this:sub-runprogram () 'Get the selected value. This will be the full file path. Strfile = mylistbox.Value 'Make sure something was selected. If lane (strfile) & gt; 0 Then run the script file ObjShell = Set CreateObject ("WScript.Shell") objShell.Run Chr (34) & amp; Struffle & amp; Note:
Chr (34)
is used to add double quotes around the filename, which contains spaces.Question 2: How do I pass a variable in the file?
This is where things become a bit tricky though you want to pass an argument in script, then you can directly
shell.ron
You can run VBSPS using the command (as we did above), you need to explicitly use it in one of the scripting engines, executable.objShell.Run "wscript.exe" & amp; Chr (34) & amp; Struffle & amp; Chr (34) & amp; "" & Amp; Chr (34) & amp; StrParam & amp; Chr (34)
Here, we use the
wscript.exe
(the "GUI" version of Windows Scripting Host) to run the script file explicitly. Are there. We are doing our file with double quotes, as we did above and, finally, we are adding a place to separate the "command" from the parameter. For completeness, we also add double quotes around the parameter, there is also an empty space.
Comments
Post a Comment